remove-item powershell cmdlet permission denied even though I have permissions

Joseph Daly
Joseph Daly used Ask the Experts™
on
I am trying to run a simple powershell remove-item cmdlet in order to delete a file from a remote system. My cmdlet is below

$present3 = test-path "\\$server\d$\Temp\vmware-SYSTEM\VMwareToolsUpgrader.exe"
if ($present3 -eq "True") {remove-item -path "\\$server\d$\Temp\vmware-SYSTEM\VMwareToolsUpgrader.exe" -force}

I have confirmed that the location and file is correct and that the file is present before the deletion. If I run the script i will be presented with a block of red text stating permission denied.

If I unc path to the same location and right click the file i can delete the file no problem. The file is set to read only i am not sure if this is a problem with the remove-item cmdlet. I have used the -force command which should make it delete the file regardless.

Also just to confirm that the script isnt the culprit i tried the one liner below with no luck as well.

remove-item -path "\\$server\d$\Temp\vmware-SYSTEM\VMwareToolsUpgrader.exe" -force

This is a new one to me. Any help is appreciated.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2015
Commented:
Hi Joseph,

Try adding the credentials to this and see if it works

$CRED = Get-Credential
$present3 = test-path "\\$server\d$\Temp\vmware-SYSTEM\VMwareToolsUpgrader.exe"
if ($present3 -eq "True") {remove-item -path "\\$server\d$\Temp\vmware-SYSTEM\VMwareToolsUpgrader.exe" -force -Credential $CRED}

Open in new window


Regards

Guy
I'm not too familiar with the credential option can you give me a little detail as to how that works.
Top Expert 2015
Commented:
Sure,

The first bit of the code
$Cred = Get-credential

Open in new window

Will cause a login prompt to appear. When you provide your details it is saved into the $CRED variable.

In the remove-item bit of code, right at the end i added
-credential $CRED

Open in new window

This then tells the command to run under the credentials you provided in the previous step.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial