Solved: Dynamic switches in PowerShell scripts

Status
Not open for further replies.
A lousy title perhaps, but I just don't know the word for what I want to do (and if I even should attempt to do it that way).

I'd like to write a script where I via a variable can set switches for some commands.

Like for example, I want to do:
Code:
foreach ($file in Get-ChildItem $folder.Path)
OR
Code:
foreach ($file in Get-ChildItem $folder.Path -recurse)
depending on a variable.

I could break it apart and have some sort of if-then-else, but the code in the foreach is the same for both variants making me want to do something like
Code:
foreach ($file in Get-ChildItem $folder.Path $recurse)
having the switch argument dynamic.

Can I do it like that in some way. And also, should I do it like that?
 
Status
Not open for further replies.