PowerShell running a system command with arguments that include "/"
I'm trying to run a simple system command: dir c:\Temp /ar from a PowerShell script using "&", Invoke-Expression, Invoke-Command but looks like PowerShell does not really like the forward slash "/". I've also tries to escape it using "" or "`" but to no avail...
Any idea?
dir
is neither a PowerShell command nor a native executable - it's a built-in command in cmd.exe - so you need to invoke cmd.exe
:
& cmd /c dir c:\Temp /ar
PowerShell aliases dir
to its own Get-ChildItem
command - and Get-ChildItem
will interpret /ar
as a rooted path argument, thus attempting to list the directory contents at /ar
(or C:\ar
on Windows), which is probably what you're currently experiencing.
To list read-only items with the Get-ChildItem
command, use the -Attributes
parameter:
Get-ChildItem -Attributes ReadOnly
Which can conveniently be shortened to:
Get-ChildItem -ar
So you can also solve your problem by simply using -ar
instead of /ar
:
dir c:\Temp -ar