Solved

POWERSHELL COMMAND FOR COPY-ITEM

Posted on 2010-09-10
8
1,230 Views
Last Modified: 2012-05-10
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
Comment
Question by:Colchester_Institute
  • 4
  • 2
  • 2
8 Comments
 
LVL 31

Expert Comment

by:DrUltima
Comment Utility
Are you wanting PowerShell to do the copying, or are you wanting PowerShell to call Robocopy?
0
 
LVL 1

Author Comment

by:Colchester_Institute
Comment Utility
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

by:soostibi
soostibi earned 250 total points
Comment Utility
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

Open in new window

0
 
LVL 31

Expert Comment

by:DrUltima
Comment Utility
That is cleaner than what I was doing... Well done, soostibi.
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 1

Author Comment

by:Colchester_Institute
Comment Utility
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

by:Colchester_Institute
Comment Utility
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

by:
soostibi earned 250 total points
Comment Utility
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

Open in new window

0
 
LVL 1

Author Comment

by:Colchester_Institute
Comment Utility
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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Join & Write a Comment

I thought I'd write this up for anyone who has a request to create an anonymous whistle-blower-type submission form created using SharePoint 2010 (this would probably work the same for 2013). It's not 100% fool-proof but it's as close as you can get…
Synchronize a new Active Directory domain with an existing Office 365 tenant
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

771 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now