Powershell move-item access denied
PowerShell 2.0 you will need to make the directory before the copy.
In PowerShell 3.0 you do not need to create, because the Move-Item or Copy-Item will make that for you.
As far as getting the files. We can use the Get-ChildItem
with the switch -Recurse
, because it will grab all the files in the first directory included the subfolders. Once we grab the files and folders we pipe it to the Move-Item
with the option -Force
. Note the Move-Item will move the files to the new directory. If this is what you want then we need to use Copy-Item
. I have also added the -Verbose
switch for debugging. Let us know what files are not working.
Function MoveTheFolder($VariableName){if ( -not (Test-Path "D:\FolderName02") ) {New-Item "D:\FolderName02\$VariableName" -Type Directory} cd D:\ #You do not need this, but its ok. Get-ChildItem -Path "D:\FolderName01\$VariableName\*" -Recurse | Move-Item -Destination "D:\FolderName02\$VariableName" -Force -VerboseGet-ChildItem -Path "D:\FolderName01\$VariableName\*" -Recurse | Copy-Item -Destination "D:\FolderName02\$VariableName" -Force -Verbose }
Let me know if this works.
So Zach's solution, while not completely correct did lead me to the actual answer. Here is the working code:
Function MoveTheFolder($VariableName){ if ( -not (Test-Path "D:\FolderName02") ) { New-Item "D:\FolderName02\$VariableName" -Type Directory } Get-ChildItem -Path "D:\FolderName01\$VariableName\" | Move-Item -Destination "D:\FolderName02\$VariableName" -Force Remove-Item -Path "D:\Folder01\$VariableName" -Force}
Got rid of the wildcard signifier and -recurse because all I want is the folder names, no need to enumerate the files therein.