I have a file storage server with documents are stored in the different locations. See file listing below. I need to move all of the items in the folder called Archive to another backup server and keep the folder structure. I tried Get-ChildItem, Move-item, Split-path but I can’t get it to work. Can someone please help me? Thank you in advance.
Here is what I tried. I just can’t get array03(which is the source path) and array04(which is destination) in a table format like SQL to loop through each for the move-item cmdlet. If this doesn’t work, I need a batch file move or copy command.
$filepath = 'C:\Documents\TestData\'
# create array01 to get all the files, including subfolders and only file type.
$array01 =@()
$array01 = Get-Childitem $filepath -Name -Recurse -File
# create array02 to match filename with word Archive
$array02 = @()
$array02 = $array01 -match 'Archive'
# create array03 to (1)get the matched items and (2)write out full path for all matched items.
$array03 = @()
#$array02 | ForEach-object {Write-Output "C:\Documents\TestData\\$_"} <--replace this hard path to variable $filepath
$array03 = $array02 | ForEach-object {Write-Output "$filepath\$_"}
# for parent path folder
$array04 = @()
$array04 = $array03 | ForEach-object {split-path $_ -parent}
# either create a batch copy file or use Move-Item
$array05 = @()
$array05 = $array03 | ForEach-object {Write-Output "Copy ""$array03"" $_" } |Format-Table
$array05 = ForEach-object {Move-Item -path $array03 -destination $array04 }
You should ALWAYS read the help for the cmdlets you’re about to use COMPLETELY including the examples to learn how to use them.
Most cmdlets, which by default do not generate any output, can be forced to output something with the parameter -PassThru. This output can be piped to a TXT or CSV file.
If that does not work you might try one of the common parameters supported by almost all cmdlets:
That happens quite often. I think it could be because you just wrapped your head around the issue explaining it to someone else (sometimes even in a foreign language) and then your brain is almost there but you do not realise it yet.