Skip to content

Instantly share code, notes, and snippets.

@1RedOne
Created December 2, 2015 15:46
Show Gist options
  • Save 1RedOne/0437b944345237889615 to your computer and use it in GitHub Desktop.
Save 1RedOne/0437b944345237889615 to your computer and use it in GitHub Desktop.
StackOverFlow - Multiple switch values inside a loop
#Make a few test files
290..296 | %{
"ham" > t:\"ham$($_)bacon.iso"}
#verify they were created
dir t:\*.iso
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 12/2/2015 10:41 AM 12 ham290bacon.iso
-a---- 12/2/2015 10:41 AM 12 ham291bacon.iso
-a---- 12/2/2015 10:41 AM 12 ham292bacon.iso
-a---- 12/2/2015 10:41 AM 12 ham293bacon.iso
-a---- 12/2/2015 10:41 AM 12 ham294bacon.iso
-a---- 12/2/2015 10:41 AM 12 ham295bacon.iso
-a---- 12/2/2015 10:41 AM 12 ham296bacon.iso
#change our code. Now, instead of seeing if the name is in a list of names we don't want, we're using
#PowerShell's wildcard search tool ( -like ), to find files with 291 or 292 somewhere in their name
$pattern = '(\d{2,4})'
Dir t:\ *.iso | Where {$_.BaseName -match $pattern} | ForEach-Object{
if (($_.BaseName -like "*291*") -or ($_.BaseName -like "*292*")) {$null}
else{$_}
}
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 12/2/2015 10:41 AM 12 ham290bacon.iso
-a---- 12/2/2015 10:41 AM 12 ham293bacon.iso
-a---- 12/2/2015 10:41 AM 12 ham294bacon.iso
-a---- 12/2/2015 10:41 AM 12 ham295bacon.iso
-a---- 12/2/2015 10:41 AM 12 ham296bacon.iso
#We see the offending files were removed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment