Hi am not able to rename a file using the below code. Please help me on this
$date = Get-Date -format "yyyyMMdd"
$path='D:\Users\user\Desktop\Working\'
$fn = $path+'xxx_'+$date+'.txt'
$tn = $path+'yyy'+$date+'.dat'
Rename-Item -Path $fn -NewName $tn
Am getting the below error.
Rename-Item : Cannot process argument because the value of argument "path" is not valid. Change the value of the "path" argument and run the operation again. At line:1 char:1 + Rename-Item -Path $fn -NewName $tn + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidArgument: (:) [Rename-Item], PSArgumentException + FullyQualifiedErrorId : Argument,Microsoft.PowerShell.Commands.RenameItemCommand
Write-Host ('$fn = {0} - $tn = {1}' -f $fn, $tn)Remove-Variable pathprior to defining path, in case something is casting$pathto something besides a[string]. I had some code in my profile caused some similar issues by not cleaning up the$pathvariable. Using alternative methods of building the string like LotPings solution work by verifying that$pathis of the expected type before use. (LotPings answer even has some extra validation to validate the path prior to use)