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

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.
LVL 35
Joseph DalyAsked:
Who is Participating?
 
Guy LidbetterCommented:
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.
0
 
Guy LidbetterCommented:
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
0
 
Joseph DalyAuthor Commented:
I'm not too familiar with the credential option can you give me a little detail as to how that works.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.