Hello experts,
I am new to powershell and I am writing a powershell script to copy certain files from temp location to target directories, this is where I am right now:
$sourcedirectory = “C:\temp\Source”
$targetdirectory = “C:\Users\Dest”
$backupdirectory = "C:\Users\Dest"
#get-location
#cd $sourcedirectory
#get-location
#Get-ChildItem -recurse | where {$.PsIsContainer} | Select-Object Name
#dir -r | % { if ($.PsIsContainer) {$_.Name} }
#Resolve-Path -relative
if (!(Test-Path -path $targetdirectory)) {New-Item $targetdirectory -Type Directory}
#Take back up of existing file
$backup_directory = New-Item -Path “$backupdirectory\backup_$(Get-Date -Format yyyyMMdd)” -Force -ItemType Directory
Write-Host Copying contents from $targetdirectory to $backup_directory
Copy-Item -Path $targetdirectory -recurse -Destination “$($backup_directory.Fullname)” -Force
#Copy files from source to destination
#Write-Host Following files needs to be copied:
$absolutePath = Get-ChildItem -Path $sourcedirectory -recurse
foreach ($item in $absolutePath)
{
Write-Host Copying $item.FullName to $targetdirectory
Copy-Item -Path $sourcedirectory/* -recurse -force -Destination $targetdirectory
}
My directory structure at source looks like below:
Source
±Client
±bin
±ext
±ext64
±lib
±Patches
±Server
±ext
±ext64
±Patches\
On Dest, same folder structure is available as well.
Using powershell command i just want the output like below:
\Source\Client\bin
\Source\Client\ext
\Source\Client\patches…
\Source\Server\ext
\Source\Server\ext24
\Source\Server\patches
The Get-ChildItems commands I have tried so far gives me the absolute path, i want output to be listed in above manner i.e remove C:\temp from the output. I will use this in my for loop for a proper message as stdout here:
Write-Host Copying $item.FullName to $targetdirectory<I will use my variable here to get proper dirlisted>
Also, please let me know if script looks ok altogether or not.