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)
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?
To rename and move an item, use Move-Item . You can't use wildcard characters in the value of the NewName parameter. To specify a name for multiple files, use the Replace operator in a regular expression. For more information about the Replace operator, see about_Comparison_Operators.
Typing CD\ causes PowerShell to move to the root directory. As you can see, you can use the CD.. or CD\ command to move to a lower level within the folder hierarchy. You can also use the CD command to enter a folder. Just type CD, followed by the folder name.
Leos Marek Wed, Aug 10 2022 Sat, Aug 13 2022 commands, powershell 31. Robocopy (Robust File Copy) is a command line folder and file replication tool available as a standard Windows feature since Windows Server 2008.
Just using -literalpath
parameter for move-item
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
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With