Link to home
Start Free TrialLog in
Avatar of nav2567
nav2567Flag 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
}
Avatar of oBdA
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

Avatar of 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
Avatar of oBdA
oBdA

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of 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.
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