IanB
IanB

Reputation: 271

Pipelined copy-item doesnt work after a file is renamed

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:

  1. Is the most recent file created
  2. It is an MP3 file
  3. It has a certain character set in the file name.

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

Answers (1)

SomeShinyObject
SomeShinyObject

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

Related Questions