remove folders remotely except some with powershell

Viewed 32

I have to delete the user profile folders from some pc, except the Administrator folder. The script I'm using is the following:


    $Computerlist = get-content C:\windows\temp\pc.txt 
Foreach ($computer in $Computerlist) 
{Get–ChildItem –Path \\$computer\c$\users\*.* –Include *.*  –Force | Where-Object $_.Name -notmatch "Administrator" | Remove-Item -Force -Recurse
}

but it return the following error:

Where-Object : Impossibile convalidare l'argomento sul parametro 'Property'. L'argomento è null o vuoto. Fornire un
argomento che non sia null o vuoto ed eseguire di nuovo il comando.
In riga:4 car:82
+ ... uter\c$\users\*.* -Include *.*  -Force | Where-Object $_.Name -ne "Ad ...
+                                                           ~~~~~~~
    + CategoryInfo          : InvalidData: (:) [Where-Object], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.PowerShell.Commands.WhereObjectCommand

Where-Object : Impossibile convalidare l'argomento sul parametro 'Property'. L'argomento è null o vuoto. Fornire un
argomento che non sia null o vuoto ed eseguire di nuovo il comando.
In riga:4 car:82
+ ... uter\c$\users\*.* -Include *.*  -Force | Where-Object $_.Name -ne "Ad ...
+                                                           ~~~~~~~
    + CategoryInfo          : InvalidData: (:) [Where-Object], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.PowerShell.Commands.WhereObjectCommand

if I run the single command on a pc it works correctly

{Get–ChildItem –Path c:\users\*.* –Include *.*  –Force | Where-Object $_.Name -notmatch "Administrator" | Remove-Item -Force -Recurse
0 Answers
Related