Tuesday 25 June 2019

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

Answer



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 ”,“”)}

No comments:

Post a Comment

How can I VLOOKUP in multiple Excel documents?

I am trying to VLOOKUP reference data with around 400 seperate Excel files. Is it possible to do this in a quick way rather than doing it m...