JoeBlow
JoeBlow

Reputation: 61

Powershell move-item access denied

Running a PS form with a button. Button press does some stuff then calls a module. I need to move a folder from D:\FolderName01\MyFolderName to D:\FolderName02 using the module.

I am logged in as Administrator. Server 2012-R2

The code does NOT run when invoked from a button press and the automated script runs.

It DOES run without problem when I am editing the code, I highlight it and use "Run Selection".

This is literally the entire module. Nothing else is being done.

Code:

 function MoveTheFolder($VariableName){

    if ( -not (Test-Path "D:\FolderName02") ) {
       md "D:\FolderName02"}

    cd -Path d:\
    Move-Item -Path "D:\FolderName01\$VariableName" -Destination "D:\FolderName02" -Force

 }

FolderName02 is created without issue. $VariableName is passed in to the module and is correct based on a breakpoint analysis and the actual error message. The original path is a DVD drive (.iso file on F:).

Error given is

move-item: Access to the path '[D:\FolderName01\$VariableName]' is denied.

I can move the folders manually using Windows Explorer, so they do not appear to be tied up with open applications.

Upvotes: 4

Views: 23261

Answers (2)

JoeBlow
JoeBlow

Reputation: 61

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.

Upvotes: 1

Zach Olinske
Zach Olinske

Reputation: 557

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 -Verbose
Get-ChildItem -Path "D:\FolderName01\$VariableName\*" -Recurse | Copy-Item -Destination "D:\FolderName02\$VariableName" -Force -Verbose
        }

Let me know if this works.

Upvotes: 1

Related Questions