PowerShell to Copy File for Google

I am having an issue where the Master_Preferences file is not getting copied to the target location. I have the constraint of creating a package for every new version and I am getting away from batch files. the post-deployment this is not working…it is supposed to check for the 64 bit structure and copy the file if the folder is there, otherwise copy the folder to the 32 bit folder structure. Thanks -

  $Master_Preferences = '$dirSupportFiles\master_preferences.file'
   $secondpath = "c:\Program Files\Google\Chrome\Application\"
   $ValidPath = test-path "c:\Program Files (x86)\Google\Chrome\Application\" -IsValid

   if ($ValidPath -eq $true ) 
    {copy-item -path "$Master_Preferences" -Destination "$ValidPath"} 
    {copy-item -path "$Master_Preferences" -Destination "$secondpath"}

Remove -IsValid from test-path. The Isvalid parameter will always return true.

From the help:


Indicates that this cmdlet tests the syntax of the path, regardless of whether the elements of the path exist. This cmdlet returns $True if the path syntax is valid and $False if it is not.

1 Like

You can use this:

$Master_Preferences = ‘$dirSupportFiles\master_preferences.file’
$secondpath = “c:\Program Files\Google\Chrome\Application”
$ValidPath = “c:\Program Files (x86)\Google\Chrome\Application”

if (test-path $ValidPath)
{copy-file -path “$Master_Preferences” -Destination “$ValidPath”}
{copy-file -path “$Master_Preferences” -Destination “$secondpath”}

This ended up working in my case, thanks for all of the great information:

$GooglePath= test-path -path “C:\Program Files (x86)\Google\Chrome\Application”
if ($GooglePath -eq $true )
{copy-item -path “$dirSupportFiles\master_preferences” -Destination “C:\Program Files
(x86)\Google\Chrome\Application” -Force }
elseif ($GooglePath -eq $false)
{Copy-Item -Path “$dirSupportFiles\master_preferences” -Destination “C:\Program
Files\Google\Chrome\Application” -force }

You dont need to do elseif. Pointless check in this case. Also for true/false you can just put it in the if statement. No need for a variable. Like this:

If (Test-Path -Path 'C:\Program Files (x86)\Google\Chrome\Application') {
    Copy-Item -Path "$dirSupportFiles\master_preferences" -Destination 'C:\Program Files (x86)\Google\Chrome\Application' -Force 
} else {
    Copy-Item -Path "$dirSupportFiles\master_preferences" -Destination 'C:\Program Files\Google\Chrome\Application' -Force