im trying to delete a specific group of directories ( all directories contain numbers as the directory name) under the c:\users directory structure. they are all beyond the 260 char limit. the only option that seems to work is “rd /s /q” command string.i had to try so many variations just to get beyong the 260 char max issue… its works if I run it locally… it works if I run it remotely from another machine while in a possession, but I cant seem to get it to work correctly in this script. its picking the correct directories… but it doesn’t seem to be running the invoke-command with the argumentlist. I have been all over the internet the past 4 days and I cant seem to get better then this. I am not understanding this aspect of it and I m looking for a little more info as to what im doing wrong.
any help/suggestions appreciated
$computerlist = get-content "C:\SCRIPTS\CLASS-LISTs\single.txt"
$mainpath = "c:\users\"
$scriptblockcontent = {
param ($delthisfolder)
"rd /s /q $delthisfolder"
}
foreach($computer in $computerlist){
try {
if (test-connection -cn $computer -erroraction silentlycontinue ){
$remotepcdir = invoke-command -computername $computer -ScriptBlock {get-childitem c:\users}
foreach ($dir in $remotepcdir) {
if ($dir.name -match '(^\d[0-9])'){
$delthisfolder = join-path -path C:\users -childpath $dir.name
write-host " "$dir" on "$computer" needs to be removed "
invoke-command -computername $computer -scriptblock {$scriptblockcontent } -ArgumentList $delthisfolder
}
else
{
write-host "$dir on $computer is not proper name... nothing will be done"
}
}
}
}
catch [TestConnectionException,Microsoft.PowerShell.Commands.TestConnectionCommand]{
write-host "may not be on or may not be reachable"
}
catch [WinRMOperationTimeout,PSSessionStateBroken]{
write-host "WINRM can not complete the operation"
}
}
So… you’ve provided a bunch of background information, but I’m not sure if all of it’s relevant to the question? Is the question just how to overcome the path character limit? If so, using purely PowerShell or .NET, you can’t, because the limitation is in .NET itself unfortunately. You could (as I think you pointed out) run external commands, which don’t have the limitation since they’re not built on .NET.
Now, in terms of running the rd command remotely, this is what you’re doing:
well… I thought I was using $scriptblock content along with the -argumentlist as a way to pass the variable for each folder…, which I thought was one of the problems I was facing. during troubleshooting… I saw feedback of each directory being written to screen so I know it found each directory and assigned it to the variable as I would like.
Im still not understanding it. your examples above look like what im doing, but it isn’t working as expected… its not deleting the directories.
ok… made the following changes…so far… this gives me the closest response that im looking for. it deletes some folders, but on some directories it gives me “directory is not empty” messages… when I look at it… I see the directory is empty, but the directory name is still there.
the reason I gave so much info was to explain my choices for doing it the way I have…deleting any other group of folders wouldn’t be a challenge… its the fact that its beyond the 260 .net limitation that is forcing me to do it this way…so the fact that its an external command has changed how I need to respond to it.
foreach ($dir in $remotepcdir) {
if ($dir.name -match '(^\d[0-9])'){
$delthisfolder = join-path -path C:\users -childpath $dir.name
write-host " "$dir" on "$computer" needs to be removed "
invoke-command -computername $computer -scriptblock {
param($first)
cmd /c "rd /s /q $first"
} -ArgumentList $delthisfolder
}
else
{