Avatar of gmbaxter
gmbaxter
Flag 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.
PowershellScripting Languages

Avatar of undefined
Last Comment
gmbaxter

8/22/2022 - Mon
remmett70

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.
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
gmbaxter

ASKER
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
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
remmett70

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.
gmbaxter

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

Hello,

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

Regards
A
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
ASKER CERTIFIED SOLUTION
gmbaxter

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
gmbaxter

ASKER
My solution worked,