copy-item-move-item or remove-item server\fileshare access denied

%3CLINGO-SUB%20id%3D%22lingo-sub-2191492%22%20slang%3D%22en-US%22%3Ecopy-item-move-item%20or%20remove-item%20server%5Cfileshare%20access%20denied%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2191492%22%20slang%3D%22en-US%22%3E%3CDIV%20class%3D%22lia-message-body%20lia-component-message-view-widget-body%20lia-component-body-signature-highlight-escalation%20lia-component-message-view-widget-body-signature-highlight-escalation%22%3E%3CDIV%20class%3D%22lia-message-body-content%22%3E%3CP%3EI%20am%20trying%20a%20lot%20of%20things%20to%20get%20the%20following%20working%3A%3C%2FP%3E%3CP%3EWe%20have%20a%20fileshares%20of%20which%20the%20structure%20is%20like%3A%3C%2FP%3E%3CP%3E%5C%5Cserver%5C%24nameshare%20(for%20example%3A%20%5C%5Ctest%5Cusername%20where%20username%20is%20a%20folder)%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ERegurlarly%20we%20must%20delete%20these%20fileshares%20so%20i%20would%20like%20to%20do%20this%20with%20powershell.%20Accually%20there%20are%202%20of%20those%20shares%20but%20if%20above%20is%20succeeding%20i%20will%20try%20that%20with%20another%20share.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThe%20problem%20is%20i%20have%20tried%20to%20copy%20the%20sharefolder%20with%20all%20of%20its%20contents%20to%20another%20networkshare%20and%20then%20remove-item%20the%20original%20foldershare.%20Then%20getting%20a%20access%20denied.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ETried%20to%20move-item%20the%20folder%2C%20access%20denied%3C%2FP%3E%3CP%3ETHen%20tried%20to%20delete%20the%20folder%20with%20cmd%20in%20powershell%20but%20cmd%20is%20not%20accepting%20the%20variable%20%24name%20for%20the%20foldersharename%20which%20i%20start%20the%20script%20with.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20have%20started%20Powershell%20as%20an%20admin%20so%20have%20enough%20rights%2C%20ik%20also%20came%20accross%20the%20fact%20that%20many%20people%20have%20this%20and%20must%20first%20take%20ownership%20of%20the%20folder%3F%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3Eparam%20(%24naam)%3CBR%20%2F%3Eif%20(%24naam%20-eq%20%24null)%7B%3CBR%20%2F%3E%24naam%20%3D%20Read-Host%20-Prompt%20%22Please%20enter%20user%20logon%20name%22%3CBR%20%2F%3E%7D%3CBR%20%2F%3Eget-aduser%20%24name%3CBR%20%2F%3ESet-ADUser%20%24name%20.........%3CBR%20%2F%3ECopy-Item%20%5C%5Cserver%5C%24name%20%5C%5Cserver%5Csharename%5Cusername%5C%20-Recurse%20-force%3CBR%20%2F%3E%23cmd%20%2Fc%20'rd%20%2Fs%20%2Fq%20%5C%5Cserver%5C%24naam'%20(this%20one%20does%20not%20accept%20%24name%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EAlso%20tried%20it%20without%20-force.%20And%20tried%20first%20the%20deletion%20of%20all%20files%20but%20keep%20geting%20access%20denied.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EIs%20it%20possible%20to%20do%20first%20a%20enter-pssession%20to%20the%20server%20and%20then%20get-childitem%26nbsp%3B%20%7C%20remote-item%3F%20but%20to%20automate%20the%20pssession%20automaticcaly%20fill%20in%20the%20server%5Csharename%20like%20i%20also%20fill%20in%20the%20username%20or%20something%20like%20that%3C%2FP%3E%3C%2FDIV%3E%3C%2FDIV%3E%3CDIV%20class%3D%22LabelsForArticle%20lia-component-labels%20lia-component-message-view-widget-labels-with-event%22%3E%3CDIV%20class%3D%22LabelsList%22%3E%3CUL%20class%3D%22lia-list-standard-inline%22%3E%3CLI%3E%26nbsp%3B%3C%2FLI%3E%3C%2FUL%3E%3C%2FDIV%3E%3C%2FDIV%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-2191492%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3Ecopy-item%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E
Occasional Contributor

I am trying a lot of things to get the following working:

We have a fileshares of which the structure is like:

\\server\$nameshare (for example: \\test\username where username is a folder)

 

Regurlarly we must delete these fileshares so i would like to do this with powershell. Accually there are 2 of those shares but if above is succeeding i will try that with another share.

 

The problem is i have tried to copy the sharefolder with all of its contents to another networkshare and then remove-item the original foldershare. Then getting a access denied.

 

Tried to move-item the folder, access denied

THen tried to delete the folder with cmd in powershell but cmd is not accepting the variable $name for the foldersharename which i start the script with.

 

I have started Powershell as an admin so have enough rights, ik also came accross the fact that many people have this and must first take ownership of the folder?

 

param ($naam)
if ($naam -eq $null){
$naam = Read-Host -Prompt "Please enter user logon name"
}
get-aduser $name
Set-ADUser $name .........
Copy-Item \\server\$name \\server\sharename\username\ -Recurse -force
#cmd /c 'rd /s /q \\server\$naam' (this one does not accept $name

 

Also tried it without -force. And tried first the deletion of all files but keep geting access denied.

 

Is it possible to do first a enter-pssession to the server and then get-childitem  | remote-item? but to automate the pssession automaticcaly fill in the server\sharename like i also fill in the username or something like that

  •  
1 Reply

@Corne1205 

if you try Set-Location \\ShareServer\Username are you able to access the folder via this command

this seems to be a permission issue and ownership