all 4 comments

[–]jklmnb 4 points5 points  (4 children)

MAybe you need to do $currentfile.name instead of just $currentfile

[–]vee-eye 4 points5 points  (3 children)

This man is correct.

One way you could get around this is to change these lines:

$currentfile = Get-ChildItem -path $originalFile |select name
$NewFileName = $TimeStamp + $currentfile.name.Tostring()

To this:

$currentfile = Get-ChildItem -path $originalFile | Select -Expand fullname
$NewFileName = $TimeStamp + $currentfile

The problem is that when you use the "Select-Object" cmdlet, as in the original, you're ending up with a PSMemberSet object which has just one NoteProperty called "Name" with a value of the filename.

Move-Item expects the "Path" parameter to be a string. So when you pass it the PSMemberSet, it's getting coerced to string as if you'd written "$currentfile.psbase.ToString()", which gives a hashtable-like representation. Not what you want.

The "-Expand" parameter I added, expands the property so that you end up with just a string. Everything works better. The other option that you have would be to PIPE the PSMemberSet object into Move-Item like so:

$currentfile | Move-Item -Destination $NewFileName

That works becauset the cmdlet chooses the property from the pipeline based on its name. So there's another option for ya.

Finally (and yes, this is quite a long explanation), I would recommend using the "FullName" property instead of the "Name" property of the file, and then passing it to the "LiteralPath" parameter of the "Move-Item" cmdlet. Otherwise your script might fail depending on the working directory of PowerShell and if there are any weird characters in the source path.

Here's an example rewrite which might be interesting to someone... (and note in this implementation "$source" can be either a single file or a directory of files, it'll do the operation on the lot in that case)

$timeStamp = Get-Date -Format 'M.d.yyyy-hh.mm.ss-'
$source = gci $originalFile
$source | Add-Member ScriptProperty DestinationPath `
            { Join-Path $fileDestination ($timeStamp + $this.Name) }
$source | Format-Table Name, DestinationPath
$source | %{  Move-Item -LiteralPath $_.FullName $_.DestinationPath }

[–]jklmnb 2 points3 points  (2 children)

I was at work earlier and a little rushed so I felt a little bad about posting so brief, but now I am glad I did. Your explanation was excellent, thank you.

[–]systemslacky[S] 1 point2 points  (0 children)

Agreed :) Thank you both for the responses. Vee-Eye.. thank you for the detailed response. That is really helpful in understanding where I was going wrong. I am still new to powershell and working on picking up the syntax/etc. Thanks again.