2

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

IanB
  • 271
  • 3
  • 13
  • Just to clarify: `Copy-Item` step works in Powershell ISE, but not in CMD or Task Scheduler? For starters, try adding `-Verbose` and `-ErrorAction Stop` to `Copy-Item` - this should give you more verbose output. – qbik Mar 16 '17 at 05:10
  • 1
    Hi, sorry the title was misleading which I have edited now. The issue is copy-item -destination $MediaDir doesn't work after the file is renamed – IanB Mar 16 '17 at 05:19

1 Answers1

4

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
SomeShinyObject
  • 7,581
  • 6
  • 39
  • 59
  • Thanks so much SomeShinyObject! That worked perfectly and I understand why too. I'm getting better at this by learning every day! – IanB Mar 16 '17 at 06:17
  • 1
    Glad to help. If I helped answer your question, be sure to click the check so it shows the question was answered. – SomeShinyObject Mar 16 '17 at 06:23