\n Hi, and welcome to the PowerShell forum! \n\n\nDon’t apologize for being a “noob” or “newbie” or “n00b.” There’s just no need – nobody will think you’re stupid, and the forums are all about asking questions. Just ask!\nUse a descriptive subject. Don't say \"Need help\" or \"PowerShell Help\", actually summarize what the problem is. It helps the rest of us keep track of which problem is which.\nDon’t post massive scripts. We’re all volunteers and we don’t have time to read all that, nor will we copy, past…\n <\/blockquote>\n<\/aside>\n\n <\/p>","upvoteCount":0,"datePublished":"2019-06-19T17:19:10.000Z","url":"https://community.spiceworks.com/t/scripting-a-massive-copy/717189/2","author":{"@type":"Person","name":"Neally","url":"https://community.spiceworks.com/u/Neally"}},{"@type":"Answer","text":"
$maindirectory = \"\\\\server\\share\"\n$destination = \"\\\\newserver\\share\"\nForEach ($item in Get-ChildItem $maindirectory -Directory) {\n\tIf ($item.name.startswith(\"2017\") {\n\t\tTry {\n\t\t\tCopy-Item \"$maindirectory\\$item\" \"$destination\\$item\" -Recurse -ErrorAction \"Stop\"\n\t\t\tRemove-Item \"$maindirectory\\$item\" -Recurse\n\t\t}\n\t\tCatch {\n\t\t\tWrite-Error \"Unable to copy $item. Did not delete.\"\n\t\t}\n\t}\n}\n<\/code><\/pre>","upvoteCount":1,"datePublished":"2019-06-19T17:24:59.000Z","url":"https://community.spiceworks.com/t/scripting-a-massive-copy/717189/3","author":{"@type":"Person","name":"michaelwray2","url":"https://community.spiceworks.com/u/michaelwray2"}},{"@type":"Answer","text":"I am getting an error. It does not seem to like that “-Directory” Says a parameter cannot be found that matches parameter name “Directory”<\/p>","upvoteCount":0,"datePublished":"2019-06-19T18:39:08.000Z","url":"https://community.spiceworks.com/t/scripting-a-massive-copy/717189/4","author":{"@type":"Person","name":"samuelsims","url":"https://community.spiceworks.com/u/samuelsims"}},{"@type":"Answer","text":"\n\n
<\/div>\n
samuelsims:<\/div>\n
\nI am getting an error. It does not seem to like that “-Directory” Says a parameter cannot be found that matches parameter name “Directory”<\/p>\n<\/blockquote>\n<\/aside>\n
You’re running an old version of Powershell, look into pstcontainer<\/p>\n
Get-ChildItem -Recurse | where-object { $_.PSIsContainer }\n<\/code><\/pre>","upvoteCount":0,"datePublished":"2019-06-19T18:45:04.000Z","url":"https://community.spiceworks.com/t/scripting-a-massive-copy/717189/5","author":{"@type":"Person","name":"Neally","url":"https://community.spiceworks.com/u/Neally"}},{"@type":"Answer","text":"This almost works except here is the folder structure:<\/p>\n
maindirectory = C:\\data\\folders\\foldername\\2017foldername1<\/p>\n
destination = C:\\data\\newfolders\\foldername\\2017foldername1<\/p>\n
I need to be able to grab the name of the folder one level up from the 2017 folder I am trying to copy. So I feel like I am missing one line.<\/p>","upvoteCount":0,"datePublished":"2019-06-19T19:41:17.000Z","url":"https://community.spiceworks.com/t/scripting-a-massive-copy/717189/6","author":{"@type":"Person","name":"samuelsims","url":"https://community.spiceworks.com/u/samuelsims"}},{"@type":"Answer","text":"
Hello Samuel, There are several ways to manage this. If you obtain the file path as an object, e.g.:<\/p>\n
$data = GetChildItem $SomeTopLevelPath -recurse\n\n<\/code><\/pre>\n$data will contain Objects and they will have Methods and properties. You can learn what these are using:<\/p>\n
$data[0] | Get-Member\n$data[1] | gm\n\n<\/code><\/pre>\ngm is the alias for Get-Member You’ll see there are means to programmatically determination if an object is a file or a directory, what its name is, parts of the Path, et al. This can be used to script your data management. If you take a path object an make it a string:<\/p>\n
$filepath = $data[0] | select -exp fullname\n\n<\/code><\/pre>\n-exp is the alias for expandProperty and returns a string. Fullname of a file object returns the entire path which you can manipulate using regular expressions to get the parent of a 2017* directory.<\/p>","upvoteCount":0,"datePublished":"2019-06-20T21:46:24.000Z","url":"https://community.spiceworks.com/t/scripting-a-massive-copy/717189/7","author":{"@type":"Person","name":"gary-m-g","url":"https://community.spiceworks.com/u/gary-m-g"}}]}}
I am wanting to create a script to accomplish the following:
Check all folder in a directory for folders that begin with “2017”
For each folder that begins with 2017, I want to copy that file to another location but with the same name of the folder it came from.
Once the file has been copied it can be deleted from the original location.
5 Spice ups
Neally
(Neally)
June 19, 2019, 5:19pm
2
samuelsims:
create a script
Sounds not too tough. What have you tried? Where are you stuck? We’re happy to help but not a script writing service.
Post what you have and we’ll go from there.
# look into get-childitem for the folder
get-childitem
# look into robocopy for the copy process / or copy-item
robocopy $source $destination
copy-item $source $destination
If you post code, please use the ‘Insert Code’ button. Please and thank you!
Hi, and welcome to the PowerShell forum!
Don’t apologize for being a “noob” or “newbie” or “n00b.” There’s just no need – nobody will think you’re stupid, and the forums are all about asking questions. Just ask!
Use a descriptive subject. Don't say "Need help" or "PowerShell Help", actually summarize what the problem is. It helps the rest of us keep track of which problem is which.
Don’t post massive scripts. We’re all volunteers and we don’t have time to read all that, nor will we copy, past…
$maindirectory = "\\server\share"
$destination = "\\newserver\share"
ForEach ($item in Get-ChildItem $maindirectory -Directory) {
If ($item.name.startswith("2017") {
Try {
Copy-Item "$maindirectory\$item" "$destination\$item" -Recurse -ErrorAction "Stop"
Remove-Item "$maindirectory\$item" -Recurse
}
Catch {
Write-Error "Unable to copy $item. Did not delete."
}
}
}
1 Spice up
I am getting an error. It does not seem to like that “-Directory” Says a parameter cannot be found that matches parameter name “Directory”
Neally
(Neally)
June 19, 2019, 6:45pm
5
You’re running an old version of Powershell, look into pstcontainer
Get-ChildItem -Recurse | where-object { $_.PSIsContainer }
This almost works except here is the folder structure:
maindirectory = C:\data\folders\foldername\2017foldername1
destination = C:\data\newfolders\foldername\2017foldername1
I need to be able to grab the name of the folder one level up from the 2017 folder I am trying to copy. So I feel like I am missing one line.
gary-m-g
(Gary M G)
June 20, 2019, 9:46pm
7
Hello Samuel, There are several ways to manage this. If you obtain the file path as an object, e.g.:
$data = GetChildItem $SomeTopLevelPath -recurse
$data will contain Objects and they will have Methods and properties. You can learn what these are using:
$data[0] | Get-Member
$data[1] | gm
gm is the alias for Get-Member You’ll see there are means to programmatically determination if an object is a file or a directory, what its name is, parts of the Path, et al. This can be used to script your data management. If you take a path object an make it a string:
$filepath = $data[0] | select -exp fullname
-exp is the alias for expandProperty and returns a string. Fullname of a file object returns the entire path which you can manipulate using regular expressions to get the parent of a 2017* directory.