Powershell variables and CMD /C

Posted on 2014-10-14
Last Modified: 2014-10-14
In an effort to improve my scripting I am trying to move more towards PowerShell but failing horribly in places (this is one of them). I had an old .bat script I used to distribute and replace files to users folders:
Robocopy C:\Policies\Current D:\Users\Technical\mike\Documents\NewPolicies /COPY:DAT /E /Z /B /R:5 /W:1 /PURGE
Robocopy C:\Policies\Current D:\Users\Technical\Ben\Documents\NewPolicies /COPY:DAT /E /Z /B /R:5 /W:1 /PURGE
Robocopy C:\Policies\Current D:\Users\Sales\Sam\Documents\NewPolicies /COPY:DAT /E /Z /B /R:5 /W:1 /PURGE
Downside is every time their is a new user I have to add a line to the script
So I thought a powershell script and ForEach-Object would be a better way to do things but I am struggling
So far I have:
Get-ChildItem -Path D:\Users\Sales | ForEach-Object -Process { $_.Name }
Which gives me the name of each user and I know to use cmd /c "Robocopy ....." to run robocopy in a cmd environment but getting the D:\Users\Sales\ bit in before and the \Documents\NewPolicies and getting a loop out of it is evading me. Any suggestions welcome (plus will using cmd /C make any variable I create null and void?) and let me know if you need more information
Question by:Dead_Eyes
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 2

Author Comment

ID: 40379585
Ok making a little progress:
$source = "D:\Source"
$Destinations = "D:\test\folder 1","D:\test\folder 2","D:\test\folder 3","D:\test\SimpleFolder1","D:\test\SimpleFolder2","D:\test\SimpleFolder3"
foreach($target in $Destinations){
robocopy $source $target /COPY:DAT /E /Z /B /R:5 /W:1 /PURGE
Now my only issue is $destinations. Any suggestions?

Author Comment

ID: 40379592
Simplified how to I turn this:
$var1 = Get-ChildItem -Path D:\Test | ForEach-Object -Process { $_.Name }

which outputs like this:

PS C:\WINDOWS\system32> $var1
Folder 1
Folder 2
Folder 3

into a variable looking like this:

"D:\test\folder 1\New","D:\test\folder 2\New","D:\test\folder 3\New","D:\test\SimpleFolder1\New","D:\test\SimpleFolder2\New","D:\test\SimpleFolder3\New"

Author Comment

ID: 40379642
Ok getting there, it now runs but no!

$source = "D:\Source"
$Destinations = Get-ChildItem -Path D:\Test | ForEach-Object -Process { $_.Name }
foreach($target in $Destinations){
" robocopy $source 'D:\test\$target\new' /COPY:DAT /E /Z /B /R:5 /W:1 /PURGE "
Has Powershell sent you back into the Stone Age?

If managing Active Directory using Windows Powershell® is making you feel like you stepped back in time, you are not alone.  For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why.

LVL 70

Accepted Solution

Qlemo earned 500 total points
ID: 40379648
As you have correctly concluded, you do not need to use cmd /c.

Regarding the folder structure, you should make sure to get only folders (? is the same as where-object and % as foreach-object):
Get-ChildItem D:\Users\Technical | ? { $_.PsIsContainer }

Open in new window

Using that to generate the target folder:
Get-ChildItem D:\Test | ? { $_.PsIsContainer } | % {
   Robocopy C:\Policies\Current "$($_.FullName)\Documents\NewPolicies" /COPY:DAT /E /Z /B /R:5 /W:1 /PURGE

Open in new window

Note that in double quotes, variables as $X and expressions like $(...) will be evaluated and replaced with the result. But "$x.y" will expand "$x", and append ".y" as a string to it, instead of getting the property y of x.
LVL 70

Expert Comment

ID: 40379651
http:#a40379642  creates a string with the command, there is no executing of that string.

Author Comment

ID: 40379653
Done :) hope this helps someone out:
$source = "D:\Source"
$Destinations = Get-ChildItem -Path D:\Test | ForEach-Object -Process { $_.Name }
ForEach ($target in $Destinations) {
cmd /c "" robocopy $source "D:\test\$target\new" /COPY:DAT /E /Z /B /R:5 /W:1 /PURGE ""

Author Closing Comment

ID: 40379656
Probably a better way than my final conclusion

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Utilizing an array to gracefully append to a list of EmailAddresses
A recent project that involved parsing Tableau Desktop and Server log files to extract reusable user queries for use in other systems. I chose to use PowerShell to gather the data, and SharePoint to present it...
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question