Check a file exist with Powershell.

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

Author

Commented:
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?
Most Valuable Expert 2018
Distinguished Expert 2018
Commented:
Leave out the "lib" at the destination:
Copy-Item -Path "\\server1\share1\lib" -Destination "${env:UserProfile}\AppData\Local\software1" -Recurse -Force 

Open in new window

Author

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

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

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