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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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


Joseph DalyAuthor Commented:
I'm not too familiar with the credential option can you give me a little detail as to how that works.
Guy LidbetterCommented:

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.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.