Reputation: 271
I am quite new to PowerShell.
I have created a PowerShell script which identifies a specific Mp3 file out of a large number of very similar files in one folder based on certain criteria:
The file is then renamed to today's date and adds some other text to the file name:
$AudioDir = "\\Server\Audio\"
$MediaDir = "\\Server2\Media\"
$LatestMP3 = Get-ChildItem -Path $AudioDir "*NEW.MP3" | Sort-Object CreationTime | Select-Object -Last 1
Get-ChildItem -path $AudioDir $LatestMP3 |Rename-Item -newname {(GET-DATE).ToString("yyyy-MM-dd") + " NewAUDIO.mp3"}
This part works perfectly but the next step does not. I want to copy that renamed file to another folder on another server ($MediaDir = "\\Server2\Media\"
)
I am trying a pipe:
Get-ChildItem -path $AudioDir $LatestMP3 |Rename-Item -newname {(GET-DATE).ToString("yyyy-MM-dd") + " NewAUDIO.mp3"} | Copy-Item -destination $MediaDir
There is no error, the file renames as expected but the Copy-Item -destination $MediaDir
does nothing.
Any help would be greatly appreciated.
Thanks
Upvotes: 2
Views: 71
Reputation: 7811
By default, the Rename-Item
CmdLet doesn't return anything. You'll have to force it to in a pipe. Use the PassThru
parameter when in the pipe and it should copy just fine.
Get-ChildItem -path $AudioDir $LatestMP3 |Rename-Item -newname {(GET-DATE).ToString("yyyy-MM-dd") + " NewAUDIO.mp3"} -PassThru | Copy-Item -destination $MediaDir
Upvotes: 4