Avatar of nav2567
nav2567
Flag for United States of America asked on

copy data using powershell.

Hello,

I am working on a PS script to copy filders, subfolders, and files from an UNC shared to a folder in a user profile only if the folder does not exist.  

May someone check the below script and advise if it will work?

$checkpath = "%userprofile%\Appdata\Roaming\app1"

if (-not (Test-Path -LiteralPath $checkpath)) {

copy-item -path \\server1\shared\apps1\*.* -destination %userprofile%\Appdata\Roaming\apps1

}
Powershell

Avatar of undefined
Last Comment
nav2567

8/22/2022 - Mon
oBdA

% doesn't work in PowerShell; you need to use the automatic $env variable.
And if you're unsure about cmdlets that actually change anything, you can usually use the -WhatIf switch to see what would happen.
So this here is in test mode and will not actually copy anything; remove the -WhatIf to run it for real.
$checkpath = "${env:AppData}\app1"
if (-not (Test-Path -LiteralPath $checkpath)) {
	Copy-Item -Path \\server1\shared\apps1\*.* -Destination $checkpath -Recurse -Force -WhatIf
}

Open in new window

Edit: Changed path to use %AppData%
Darrell Porter

$checkpath = "${env:UserProfile}\Appdata\Roaming\app1"

If (Test-Path -Path \\Server1\Shared\Apps1 -pathtype Container) {
    if (-not (Test-Path -LiteralPath $checkpath -pathtype Container)) {
        copy-item -path \\server1\shared\apps1 -destination $checkpath -recurse -force -verbose
    }
    Else {
        Write-Output "Destination folder already exists."
    }
}
Else {
    Write-Output "Path \\Server1\Shared\Apps1 does not exist or is inaccessible."
}

Open in new window

This should do what you want it to do
nav2567

ASKER
Thanks All.  

Darrell, I have tried your script but it only create the APP1 file in the destination folder instead of the APP1 folder with the data.
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
oBdA

Just noticed that either your $checkpath or the copy command is probably: in checkpath, you have "app1", in the copy command, you used apps1.
$checkpath = "${env:AppData}\apps1"
if (-not (Test-Path -LiteralPath $checkpath)) {
	Copy-Item -Path \\server1\shared\apps1\*.* -Destination $checkpath -Recurse -Force -WhatIf
}

Open in new window

nav2567

ASKER
oBdA, I have tried.  Nothing is created...
oBdA

You noticed the part about the test mode above and that the output should say something like
What if: Performing the operation "Copy Directory" ...
This means that Copy-Item is in "pretend" mode. You need to remove the -WhatIf at the end of line 3 to run it for real.
You can as well replace it with a -Verbose to see which files are being copied.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
nav2567

ASKER
Same result as Darrell's script.  I only see the app1 file instead of the folder with content.  

Thanks.
ASKER CERTIFIED SOLUTION
oBdA

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
nav2567

ASKER
Many thanks!!!