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

Check a file exist with Powershell.

Hello,

I am working on this PS script to copy some data from a share drive to a folder in the user's profile.  Prior to the copy, I need to make sure the "install-finished.txt" file is not in the desktop folder first.  

Please advise if the below will work.  Thanks very much!


if (-not (Test-Path $env:USERPROFILE\desktop\install-finished.txt)) {
copy-item -path "\\server1\share1" - destination ${env:UserProfile}\AppData\Local\software1 -recurse -force
}
Powershell

Avatar of undefined
Last Comment
oBdA

8/22/2022 - Mon
oBdA

There's a space between the dash and "Destination" that shouldn't be there.
And note that you can test most cmdlets changing something using the -WhatIf argument.
This will not actually copy anything; remove the -WhatIf at the end to run it for real:
if (-not (Test-Path -Path "${env:USERPROFILE}\desktop\install-finished.txt")) {
	Copy-Item -Path "\\server1\share1" -Destination "${env:UserProfile}\AppData\Local\software1" -Recurse -Force -WhatIf
}

Open in new window

nav2567

ASKER
It is working.  Thank you.  

One more related question please.

I added another line to the script: Copy-Item -Path "\\server1\share1\lib" -Destination "${env:UserProfile}\AppData\Local\software1\lib" -Recurse -Force and it works.  

Inside the lib folder, there are subfolders and files.  

I test deleting two subfolders under lib, and I logout and login again.  I was expecting the two subfolders are copied back into lib.  But instead, another lib folder is created under lib and everything is copied again.  

For example, the lib folder contains subfolder1 and subfolder2.....  After I deleted subfolder1 and subfolder2, logout and login, I am seeing ...\lib\lib\subfolder1 and ....\lib\lib\subfolder2.  There is an extra lib folder in lib.  

Is there a way to fix?
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
I think I have tried that.  If I leave out the "lib", the subfolder1 and subfolder2 will be under ${env:UserProfile}\AppData\Local\software1 instead of ${env:UserProfile}\AppData\Local\software1\lib

I need lib in software1 of the target computer.
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

Can't reproduce, sorry; started out with an empty folder b, copied, removed a subfolder, copied again; everything as expected:
PS C:\> gci a -Recurse | select -expa FullName
C:\a\lib
C:\a\lib\foo
C:\a\lib\foo.txt
C:\a\lib\foo\bar.txt
PS C:\> gci b -Recurse | select -expa FullName
PS C:\> Copy-Item -Path "C:\a\lib" -Destination "C:\b" -Recurse -Force
PS C:\> gci b -Recurse | select -expa FullName
C:\b\lib
C:\b\lib\foo
C:\b\lib\foo.txt
C:\b\lib\foo\bar.txt
PS C:\> Remove-Item C:\b\lib\foo -Recurse -Force
PS C:\> gci b -Recurse | select -expa FullName
C:\b\lib
C:\b\lib\foo.txt
PS C:\> Copy-Item -Path "C:\a\lib" -Destination "C:\b" -Recurse -Force
PS C:\> gci b -Recurse | select -expa FullName
C:\b\lib
C:\b\lib\foo
C:\b\lib\foo.txt
C:\b\lib\foo\bar.txt
PS C:\>

Open in new window