nav2567
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\i nstall-fin ished.txt) ) {
copy-item -path "\\server1\share1" - destination ${env:UserProfile}\AppData \Local\sof tware1 -recurse -force
}
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\i
copy-item -path "\\server1\share1" - destination ${env:UserProfile}\AppData
}
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}\AppDat a\Local\so ftware1\li b" -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?
One more related question please.
I added another line to the script: Copy-Item -Path "\\server1\share1\lib" -Destination "${env:UserProfile}\AppDat
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I think I have tried that. If I leave out the "lib", the subfolder1 and subfolder2 will be under ${env:UserProfile}\AppData \Local\sof tware1 instead of ${env:UserProfile}\AppData \Local\sof tware1\lib
I need lib in software1 of the target computer.
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:\>
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:
Open in new window