Quantcast
Channel: PowerShell.com – PowerShell Scripts, Tips, Forums, and Resources
Viewing all articles
Browse latest Browse all 6937

Trying to create a Powershell script to copy a user profile folder and all of the folders inside to another location

$
0
0

Hi all,

I'm trying to create a Powershell script to copy a user profile folder and all of the folders inside to another location.

I have made this script and for some odd reason it only copies the folders and not the content inside?  Also for example the AppData folder is hidden and it's not copied over?

Any help would be much appreciated as I have been trying to figure this out for hours now.

$destination = "C:\Migration"

$location = "C:\Users\test"

function processCopy

{

 

 $files = Get-ChildItem -Path:$location

 foreach($file in $files)

 {

  $path = $file.FullName

  if($file.Name -ne "copy.ps1"){

  Copy-Item -Path:$path -Destination:$destination -Force -Recurse -Confirm:$false

  Write-Host "Item" $file.Name " has been sucessfully copied to" $destination

  }

  if($file.Attributes -eq "Directory")

  {

   copyFile $file $destination

  }

 }

}

 

function copyFile($directory, $target)

{

 $target +=  $directory.Name

 $files = Get-ChildItem -Force -Recurse -Path:$directory.FullName

 foreach($file in $files)

 {

  $path = $file.FullName

  if($file.Name -ne "copy.ps1"){

  Copy-Item -Path:$path -Destination:$target -Force -Recurse -Confirm:$false

  Write-Host "Item" $file.Name " has been sucessfully copied to" $target

  }

 

  if($file.Attributes -eq "Directory")

  {

   copyFile $file $target

  }

 }

}

#main

processCopy


Viewing all articles
Browse latest Browse all 6937

Trending Articles