Link to home
Start Free TrialLog in
Avatar of gmbaxter
gmbaxterFlag for United Kingdom of Great Britain and Northern Ireland

asked on

PowerShell Script to move files

Hi,

I'm trying to move folders (from a list) from one folder to another. I found this powershell script on technet:

$listPath = [Populate with the path to your CSV file]
$homepath = "C:\homefolder"
$archive = "C:\temp"
$list = import-csv $listPath
foreach($user in $list)
{
    $path = Join-Path $homepath -childpath $user.username
    move-item $path $archive
}

Open in new window


But on testing, eg

$homepath = K:\Test
$archive = K:\Archive


The whole "Test" folder gets moved into the Archive folder. I just want the user's folder and contents within \Test to move into \Archive.

Any ideas?

Thanks.
Avatar of remmett70
remmett70
Flag of United States of America image

Sounds like the Join-Path is not creating the $path variable you are trying for?

have you tried the script in the ISE and toggle it to pause so you can verify what the $path is being created as what you expect.
Avatar of gmbaxter

ASKER

Thanks for the quick reply. Thats what I thought, although I now wonder if it is not reading the csv file in correctly. I'm unsure what ISE is, but i have modified the script to echo what it is doing:

$listPath = "C:\MoveKtoR.csv"
$homepath = "K:\Test\"
$archive = "K:\Leavers\"
$list = import-csv $listPath
foreach($user in $list)
{
    $path = Join-Path $homepath -childpath $user.username
    echo $path $archive
}

Open in new window


It echoes:

path = K:\Test
archive = K:\Leavers

when it should be:

path = K:\Test\username
removing the .username from line 7 echoes:

PS C:\Users\Administrator\Desktop> .\moveKtoRLeavers.ps1
K:\Test\@{user=12345}
K:\Leavers\
K:\Test\@{user=678910}
K:\Leavers\

Better, but no idea why the @{} user= are there
here is about the ISE
http://technet.microsoft.com/en-us/library/dd315244.aspx

Add a variable and echo it before the Join-Path ($username = $user.username) to see what the childpath is being set as.
PS C:\Users\Administrator\Desktop> .\moveKtoRLeavers.ps1
K:\Test\@{user=12345}
K:\Leavers\
K:\Test\@{user=678910}
K:\Leavers\
Hello,

You should change  $user.username to  $user.user

Regards
A
ASKER CERTIFIED SOLUTION
Avatar of gmbaxter
gmbaxter
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
My solution worked,