Powershell script to copy files and folders to either 32bit program files or 64bit program files

Guys, Im very new to powershell and need your help.

I need the following script to check if the architecture is either 32 or 64 bit then copy files and folders from a specific location to which ever the program files folder is current.
ie if the arch is 64 bit then the "Program Files" folder is current.

Here is my script

$Computers = Get-Content -Path C:\4ps\implementation\Input_list.txt
$file2copy = "C:\Temp\Darren\New Text Document.txt"
$Computers | %{
write-output $_
      $pc = Get-WmiObject -computername $_ -class Win32_OperatingSystem
      if($pc.OSArchitecture -eq '64-bit'){
            $destdir = "\\$Computers\C$\Program Files\Microsoft Dynamics NAV\110\RoleTailored Client"
            if(!( Test-Path -Path $destdir)){
                  md $destdir | out-null
            }
            write-output "copy to $destdir"
            Copy-Item $file2copy $destdir      
      }
        if($pc.OSArchitecture -eq '32-bit'){
            $destdir1 = "\\$Computers\C$\Program Files (x86)\Microsoft Dynamics NAV\110\RoleTailored Client"
            if(!( Test-Path -Path $destdir1)){
                  md $destdir1 | out-null
            }
            write-output "copy to $destdir1"
            Copy-Item $file2copy $destdir1
      }
}

The problem with this script is that
1. If you add more than 1 computer the lilst it errors
2. it doesnt copy the files to which ever architecture the pc is on it just copies to the first in the script.
3. it doesnt copy any folders just the files

can anyone help

Regards
DarrenJacksonAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jose Gabriel Ortega CastroCEOCommented:
Have you tried this script?
https://gallery.technet.microsoft.com/scriptcenter/Copy-Files-from-folderA-to-29710ef9/view/Discussions

Let me know if you had issues with that script so I can correct or do modifications accordingly
0
DarrenJacksonAuthor Commented:
Hi I think this might be what Im after

$computerlist = Get-Content -Path C:\4ps\implementation\Input_list.txt
$file2copy = "C:\Temp\Darren"  #Will copy folder and contents
#$file2copy64 = "C:\Temp\Darren64"  #Will copy folder and contents
foreach ($computer in $computerlist) {
   
  $ARCH=(gwmi -Computer $computer -Query "Select OSArchitecture from Win32_OperatingSystem").OSArchitecture
 
  If ($ARCH -eq "32-bit")
  {$destdir = "\\$Computer\C$\Program Files\Microsoft Dynamics NAV\110\RoleTailored Client"
  #$ARCH + " "+ $destdir }
            if(!( Test-Path -Path $destdir)){
                  md $destdir | out-null
            }
            write-output "copy to $destdir"
            Copy-Item $file2copy $destdir -Recurse -Force      }
  Else
  {$destdir1 = "\\$Computer\C$\Program Files\Microsoft Dynamics NAV\110\RoleTailored Client"
   #$ARCH +" "+ $destdir1}
          if(!( Test-Path -Path $destdir1)){
            md $destdir1 | out-null
          }
          write-output "copy to $destdir1"
          Copy-Item $file2copy $destdir1 -Recurse -Force      }
   
}
0
footechCommented:
If the Windows OS is 32-bit, then there won't be a Program Files (x86) folder to copy to.  That folder only exists on 64-bit OS.  So it sounds like you want to copy to "c:\Program Files" all the time.
0
Has Powershell sent you back into the Stone Age?

If managing Active Directory using Windows Powershell® is making you feel like you stepped back in time, you are not alone.  For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why.

DarrenJacksonAuthor Commented:
yeah I do but what ive seen is that on certain machines that are running 64bit the same program is running from the (x86) folder and I was assuming that this was a given on all 64bit machines but its not.

Ive only just discovered this since posting im now wondering what on the OS is telling the software to run as 32bit on a 64bit.

This may well be over n above the post I havr on here and need to take a step back and work this out
0
David Johnson, CD, MVPOwnerCommented:
$Computers = Get-Content -Path C:\4ps\implementation\Input_list.txt
$file2copy = "C:\Temp\Darren\New Text Document.txt"
foreach ($computer in $computers){
  write-output $computer
  if (   test-path "\\$computer\c$\Program Files (x86)"  ) {
    $destdir1 = "\\$Computer\C$\Program Files (x86)\Microsoft Dynamics NAV\110\RoleTailored Client"
  }
  else {
        $destdir = "\\$Computer\C$\Program Files\Microsoft Dynamics NAV\110\RoleTailored Client"
        }
  if(!( Test-Path -Path $destdir)){ mkdir -Path $destdir | out-null }
  write-output "copy to $destdir"
  Copy-Item $file2copy $destdir      
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
DarrenJacksonAuthor Commented:
Thanks guys I think David has hit the nail on the head. Sometimes its just best to simplify and not over work this.
0
DarrenJacksonAuthor Commented:
Thankyou  for the help
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.