Powershell script works with -whatif but fails when running for real

By itsupport ·

I'm looking for some help with an administrative script I've written in Powershell. I'm a newbie to using Powershell but have written lots of vbscripts but couldn't use it in this instance.

My script looks like this:

$Path = "D:\StudentFolders"
$Dir = get-childitem $Path -recurse | where {$_.extension -eq ".swf"} | where{$_.Name -notmatch "button*"} | move-item -destination {join-path -path 'D:\File_server\Flash_Items' -childpath $_.FullName.Substring(17) }

I've used these links to construct the script:

The purpose of the script is find .swf files, check that they match certain criteria in the name and then move that file to a different location but maintaining the folder path. When I run the script with the -whatif parameter the script runs through showing that it would move the file from its source to its destination and maintaining the folder structure. However when I remove the -whatif it fails saying:
Move-Item : Could not find a part of the path.
At C:\Powershell\script.ps1:2 char:119
+ $Dir = get-childitem $Path -recurse | where {$_.extension -eq ".swf"} | where{$_.Name -notmatch "button
m <<<< -destination {join-path -path 'D:\File_server\Flash_Items' -childpath $_.FullName.Substring(17) }

If I remove the join-path cmdlet and just specify a driectory to put all the files the script also works so I wonder whether it's how the script is handling the string element of the Files ie not getting the Fullname property of the file to get the string object so it can be manipulated. However, as I said I'm a newbie to Powershell so would appreciate any help people could give to get this working.

Thank you

This conversation is currently closed to new comments.

2 total posts (Page 1 of 1)  
| Thread display: Collapse - | Expand +

All Answers

Share your knowledge
Back to Software Forum
2 total posts (Page 1 of 1)  

Related Discussions

Related Forums