copy data using powershell.

nav2567
nav2567 used Ask the Experts™
on
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

}
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2018
Distinguished Expert 2018

Commented:
% 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 PorterEnterprise Business Process Architect

Commented:
$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

Author

Commented:
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.
Most Valuable Expert 2018
Distinguished Expert 2018

Commented:
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

Author

Commented:
oBdA, I have tried.  Nothing is created...
Most Valuable Expert 2018
Distinguished Expert 2018

Commented:
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.

Author

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

Thanks.
Most Valuable Expert 2018
Distinguished Expert 2018
Commented:
Sorry, my bad. Was focusing too much on the target; the issue is the source.
Since you want to copy the complete folder, remove the wildcard:
$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

Author

Commented:
Many thanks!!!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial