2

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

2 Answers 2

3

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 ”,“”)}
2
  • how to this on remote computer?
    – Dan
    Jun 26, 2015 at 19:02
  • @Dan : simply add the code provided by Merwyn into your scriptblock. This will then be ran remotely. Pay attention his code is an example, this may not produce the expected result.
    – Ob1lan
    Jun 26, 2015 at 20:42
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 ".

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .