Kaseya Community

Powershell: Remove-item question

This question is answered

Can someone assist me with this? I'm pretty fresh to powershell but I enjoy playing with it. Can someone please tell me why this command wont work? It's driving me bonkers.

Remove-Item -path $path13 -force -recurse -whatif

This is the error I get. It's like it doesn't quite understand that the $path13 isn't a command, it's the path. I don't necessarily need to use these variables but I would like to learn why it's not working. Smile

I could plug in the address directly and it works just fine.

C:\Users\*\Appdata\Local\Temp\* : The term 'C:\Users\*\Appdata\Local\Temp\*' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At C:\Users\rshaw\Desktop\SCRIPTS\DskCleanup101318\dskcleanup001.ps1:13 char:11
+ $path13 = C:\Users\*\Appdata\Local\Temp\*
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (C:\Users\*\Appdata\Local\Temp\*:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException

Remove-Item : Cannot bind argument to parameter 'Path' because it is null.
At C:\Users\rshaw\Desktop\SCRIPTS\DskCleanup101318\dskcleanup001.ps1:23 char:19
+ Remove-Item -path $path13 -force -recurse -whatif
+ ~~~~~~~
+ CategoryInfo : InvalidData: (:) [Remove-Item], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.RemoveItemCommand

Verified Answer
  • Put your path in quotes so that it actually parses as a string.

    $path13 = "c:\users\*\AppData\Local\temp\*"

All Replies
  • Your Script looks fine and works for me. I believe you are just running into an issue with environment variables or aliases.

    Try another PC or using PowerShell ISE.

  • Put your path in quotes so that it actually parses as a string.

    $path13 = "c:\users\*\AppData\Local\temp\*"

  • You the man Jonathan! That was my problem. For anyone else not clear on the answer, the information I gave was actually correct. The information I left out was wrong. This is what I had:

    $path13 = C:\Users\*\Appdata\Local\Temp\*

    This is what it should have been:

    $path13 = "C:\Users\*\Appdata\Local\Temp\*"