-1

I'm trying to figure out if it's possible to use the parameter -Recurse into Get-ChildItem cmdlet depending on a value of another variable.

I mean, let's say that we have the boolean parameter $flag, if $flag is True I would like to insert the parameter -Recurse into Get-ChildItem execution, otwerwise not.

Many Thanks.

Manu
  • 1,685
  • 11
  • 27

1 Answers1

1

Switch parameters do allow for a boolean to be passed in if used with a colon ':'

Example:

$flag = $true
Get-ChildItem -Recurse:$flag

Will use -Recurse

$flag = $false
Get-ChildItem -Recurse:$flag

Will NOT use -Recurse

Shaneis
  • 1,065
  • 1
  • 11
  • 20
  • I don't see this technique being used very much, seems to be something from "the old days". – Snak3d0c Nov 23 '17 at 11:48
  • Hadn't really noticed tbh. "From the old days" seems to imply that there's a new way...share the knowledge @Snak3d0c , I want to know! :D – Shaneis Nov 23 '17 at 11:52
  • That's not what i meant hehe. I'm just saying that this technique isn't been used that often anymore (or so it seems). I used to come across it a bit more. – Snak3d0c Nov 23 '17 at 12:55
  • I use it quite often with -Confirm – StephenP Nov 24 '17 at 03:37