Path too long when deleting from powershell on remote computer
What is the easiest way to delete the file on remote computer with powershell if name longer than 260 chars? I have the following code but I am hitting 260 chars limit.
$s = New-PSSession -computername Server1
Invoke-Command -session $s -scriptblock
{Remove-Item "C:\Jenkins\workspace\Long Path with spaces" -force
-recurse}
Remove-PSSession $s
Solution 1:
This is a well-known limitation in Powershell, unfortunatelly.
This question has already been answered in StackExchange, the acepted answer was to use cmd
and dir
instead, to gather the folders list.
This answer also provided the following reference link : http://asysadmin.tumblr.com/post/17654309496/powershell-path-length-limitation
wich basically explains why dir
could do the trick in such a case, and showed the following example :
The Dir version is longer since you have to strip the extra information from the results.
$folders = cmd /c dir C:\Users /s /-c /a:h /a:d
$folders = $folders -match “Directory”
$folders = $folders | %{$_.Replace(“ Directory of ”,“”)}
Solution 2:
Did you try using a wildcard?
Remove-Item "C:\Jenkins\workspace\Long Path with *"
That will delete all files in that folder that start with "Long Path with ".