Question:
Quick question about moving items with PowerShell: does anyone know why the following script does not work when the filename has the [ or ] chars on it? (ex.: file1[VT].txt)
1 2 3 4 5 6 7 8 |
ls j:\ | foreach { $itemName = $_.Name.Replace('.', ' ') $destination = ls | where { $itemName -match $_.Name } | select -First 1 if ($destination -ne $null) { mi $_.PSPath $destination.PSPath -Verbose -WhatIf } } |
For instance, it will move the file if it’s called file1.txt but it will simply ignore files named file1[VT].txt. I’m under the assumption that it’s not finding the path to the file when it has chars [ or ] on its name. Any ideas?
Answer:
Just using -literalpath
parameter for move-item
1 2 3 4 5 6 7 8 |
ls j:\ | foreach { $itemName = $_.Name.Replace('.', ' ') $destination = ls | where { $itemName -match $_.Name } | select -First 1 if( $destination -ne $null){ mi -literalpath $_.PSPath $destination.PSPath -Verbose -WhatIf } } |