Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17


Powershell variables and CMD /C

Posted on 2014-10-14
Medium Priority
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 "
Are You Ready for GDPR?

With the GDPR deadline set for May 25, 2018, many organizations are ill-prepared due to uncertainty about the criteria for compliance. According to a recent WatchGuard survey, a staggering 37% of respondents don't even know if their organization needs to comply with GDPR. Do you?

LVL 71

Accepted Solution

Qlemo earned 2000 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 71

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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

This script can help you clean up your user profile database by comparing profiles to Active Directory users in a particular OU, and removing the profiles that don't match.
In the absence of a fully-fledged GPO Management product like AGPM, the script in this article will provide you with a simple way to watch the domain (or a select OU) for GPOs changes and automatically take backups when policies are added, removed o…
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…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

688 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