[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
Solved

# POWERSHELL COMMAND FOR COPY-ITEM

Posted on 2010-09-10
Medium Priority
1,282 Views
Hi there

I'm looking for a powershell command that is equivalent to the following dos command:

for /f %%N in (test.txt) DO CALL robocopy "D:\User Areas\Reenrolled\%%N\My Documents" "\\SERVER\E$\Area\%%N\My Documents\sub folder" /E /R:0 > C:\diditwork.txt I'm sure its something to do wiht assigning a variable somewhere but my mind has gone blank Many thanks 0 Question by:Colchester_Institute [X] ###### 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 • 4 • 2 • 2 8 Comments LVL 31 Expert Comment ID: 33646250 Are you wanting PowerShell to do the copying, or are you wanting PowerShell to call Robocopy? 0 LVL 1 Author Comment ID: 33646342 Powershell to do the copying...Have a list of names in the text file that need to be used so for example the Text file might contain: DrUltima Joeblogs MickyMouse and i need those names to be used as the areas that are being copied is: Server1\E:\Users\<Txt file name here eg Joeblogs>\My Docs and its being copied to Server2\E:\Users\<Txt file name here eg Joeblogs>\My Docs\Subfolder Like i said i know i can do this using robocopy i'd just rather start using Powershell to do such things and am sure i'm just being dense...Its Friday arvo! LOL 0 LVL 13 Assisted Solution soostibi earned 1000 total points ID: 33647840 Update the variables in lines 3-5 and the path to the text file in line 8 and the last, and try this: Import-Module bitstransfer$sourcedir = "D:\User Areas\Reenrolled"
$upload = "\\SERVER\E$\Area"
$MyDocuments = "My Documents"$subfolder = "sub folder"

$ProgressPreference = 'SilentlyContinue' Get-Content c:\test.txt | ForEach-Object {$currfolder = $_ if(!(Test-Path "$upload\$currfolder\$Mydocuments\$subfolder")){ New-Item -Path "$upload\$currfolder\$Mydocuments" -Name $subfolder -ItemType directory >$null
}
if(Test-Path "$sourcedir\$currfolder\$mydocuments"){ Get-ChildItem "$sourcedir\$currfolder\$mydocuments" -Recurse | ForEach-Object {
Start-BitsTransfer -Source $_.fullname -Destination "$upload\$currfolder\$MyDocuments\$subfolder\" -TransferType upload if($?){"successfully copied: $($_.fullname)"}else{"error during copy: $($_.fullname)"}
}
}
} > C:\diditwork.txt

0

LVL 31

Expert Comment

ID: 33647879
That is cleaner than what I was doing... Well done, soostibi.
0

LVL 1

Author Comment

ID: 33660530
Hi soostibi

That works perfect. ty..Just one more question incase i decide to add it or change it later.....How do i either make it move the original files or once its copied them delete the original files?

Many thanks
0

LVL 1

Author Comment

ID: 33660861
Also one thing i did notice.....Any subfolders weren't copied over...The files were but not the folders so the Files just ended up in one big folder for each user
0

LVL 13

Accepted Solution

soostibi earned 1000 total points
ID: 33666733
This is an updated version (I do not exactly understand the role of your "subfolder").
Import-Module bitstransfer

$sourcedir = "D:\User Areas\Reenrolled"$upload = "\\SERVER\E$\Area"$MyDocuments = "My Documents"
$subfolder = "sub folder"$ProgressPreference = 'SilentlyContinue'
Get-Content c:\ee\test.txt | ForEach-Object {
$currfolder =$_
if(Test-Path "$sourcedir\$currfolder\$mydocuments"){ Get-ChildItem "$sourcedir\$currfolder\$mydocuments" -Recurse | ?{!$_.psiscontainer} | ForEach-Object {$newfullpath = ($_.directoryname -replace ([regex]::Escape("$sourcedir\$currfolder\$mydocuments")),
"$upload\$currfolder\$MyDocuments\$subfolder")
if(!(Test-Path $newfullpath)){ New-Item -Path (split-path$newfullpath -parent) -Name (split-path $newfullpath -leaf) -ItemType directory >$null
}
Start-BitsTransfer -Source $_.fullname -Destination$newfullpath -TransferType upload
if($?){"successfully copied:$($_.fullname)"}else{"error during copy:$(\$_.fullname)"}
}
}
} > C:\diditwork.txt

0

LVL 1

Author Comment

ID: 33670588
The role of the sub folder is basically that i'm moving data from what was an old domain and placing it into the users area on the new domain.  But what them to easily be albe to find the data from the old one :-) Many thanks
0

## Featured Post

Question has a verified solution.

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

While rebooting windows server 2003 server , it's showing "active directory rebuilding indices please wait" at startup. It took a little while for this process to complete and once we logged on not all the services were started so another reboot is …
My attempt to use PowerShell and other great resources found online to simplify the deployment of Office 365 ProPlus client components to any workstation that needs it, regardless of existing Office components that may be needing attention.
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
###### Suggested Courses
Course of the Month13 days, 20 hours left to enroll