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?
 
David Johnson, CD, MVPConnect With a Mentor OwnerCommented:
$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
 
Jose Gabriel Ortega CConnect With a Mentor CEO J0rt3g4 Consulting ServicesCommented:
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
Making Bulk Changes to Active Directory

Watch this video to see how easy it is to make mass changes to Active Directory from an external text file without using complicated scripts.

 
footechConnect With a Mentor Commented:
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
 
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
 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.