deleting profiles while skipping admin

Hello folks,
i have created the following script to remove profiles from PCs, however what change would i make to skip the administrator profile?

$CLIENT = Get-Content "c:\Temp\computers.txt"
 
foreach($PC in $CLIENT){
    # Testing PC connectivity
    if(Test-Connection $PC -Count 1 -Quiet){
        # Gathering users from c:\users
        foreach($PCuser in (Get-ChildItem "\\$pc\c$\users" -Directory)){
            # delete  recursively
            RD "\\$PC\c$\Users\$($pcuser.name)\Desktop\*.*"
            RD "\\$PC\c$\Users\$($pcuser.name)\Downloads\*.*"
            RD "\\$PC\c$\Users\$($pcuser.name)\Documents\*.*"
            RD "\\$PC\c$\Users\$($pcuser.name)\Music\*.*"
            RD "\\$PC\c$\Users\$($pcuser.name)\Pictures\*.*"
            RD "\\$PC\c$\Users\$($pcuser.name)\Videos\*.*"
        }
    }else{
        Write-Warning "$PC is offline"
    }
} 

Open in new window


or if anybody has a a batch that'd be great too

Thanks for looking
LVL 3
ivan rosaAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jose Gabriel Ortega CastroCEOCommented:
$CLIENT = Get-Content "c:\Temp\computers.txt"
function Remove-Recursively{
    [CmdletBinding()]
    param(
        [Parameter(position=0,Mandatory=$true)]$Path
    )
    BEGIN{}
    PROCESS{
        #Get Files and remove them
        $Files = [System.IO.Directory]::GetFiles($Path)
        $ProfileFolders = [System.IO.Directory]::GetDirectories($Path)
        if($ProfileFolders.Count -gt 0){
            foreach($profileFolder in $ProfileFolders){
                Remove-recursively -path $profilefolder
            }
        }
        

        if($Files.Count -gt 0){
            foreach($file in $Files){
                [System.IO.File]::Delete($file)
            }
        }
        [System.IO.Directory]::Delete($Path)
    }
    END{}
}
 
foreach($PC in $CLIENT){
    # Testing PC connectivity
    if(Test-Connection $PC -Count 1 -Quiet){
        # Gathering users from c:\users
        $Folders= [System.IO.Directory]::GetDirectories("\\$pc\C$\users")
        $Filtered = $Folders | where { $_ -notmatch "Admin"}

        foreach($PCuser in $Filtered){
            Remove-Recursively -path $pfolder
        }
    }else{
        Write-Warning "$PC is offline"
    }
} 

Open in new window

0
ivan rosaAuthor Commented:
Hi Jose, let's make it easier, what about to delete the entire profiles while skipping the admin & defaults?

unfortunate i tried your script and it gave me the error below
Remove-Recursively : Cannot bind argument to parameter 'Path' because it is null.
At C:\Users\johndoe\Desktop\removeprofile2.ps1:37 char:38
+             Remove-Recursively -path $pfolder
+                                      ~~~~~~~~
    + CategoryInfo          : InvalidData: (:) [Remove-Recursively], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Remove-Recursively
 
Remove-Recursively : Cannot bind argument to parameter 'Path' because it is null.
At C:\Users\johndoe\Desktop\removeprofile2.ps1:37 char:38
+             Remove-Recursively -path $pfolder
+                                      ~~~~~~~~
    + CategoryInfo          : InvalidData: (:) [Remove-Recursively], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Remove-Recursively
 
Remove-Recursively : Cannot bind argument to parameter 'Path' because it is null.
At C:\Users\johndoe\Desktop\removeprofile2.ps1:37 char:38
+             Remove-Recursively -path $pfolder
+                                      ~~~~~~~~
    + CategoryInfo          : InvalidData: (:) [Remove-Recursively], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Remove-Recursively
 
Remove-Recursively : Cannot bind argument to parameter 'Path' because it is null.
At C:\Users\johndoe\Desktop\removeprofile2.ps1:37 char:38
+             Remove-Recursively -path $pfolder
+                                      ~~~~~~~~
    + CategoryInfo          : InvalidData: (:) [Remove-Recursively], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Remove-Recursively
 
Remove-Recursively : Cannot bind argument to parameter 'Path' because it is null.
At C:\Users\johndoe\Desktop\removeprofile2.ps1:37 char:38
+             Remove-Recursively -path $pfolder
+                                      ~~~~~~~~
    + CategoryInfo          : InvalidData: (:) [Remove-Recursively], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Remove-Recursively
 
Remove-Recursively : Cannot bind argument to parameter 'Path' because it is null.
At C:\Users\johndoe\Desktop\removeprofile2.ps1:37 char:38
+             Remove-Recursively -path $pfolder
+                                      ~~~~~~~~
    + CategoryInfo          : InvalidData: (:) [Remove-Recursively], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Remove-Recursively
 
Remove-Recursively : Cannot bind argument to parameter 'Path' because it is null.
At C:\Users\johndoe\Desktop\removeprofile2.ps1:37 char:38
+             Remove-Recursively -path $pfolder
+                                      ~~~~~~~~
    + CategoryInfo          : InvalidData: (:) [Remove-Recursively], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Remove-Recursively
 
Remove-Recursively : Cannot bind argument to parameter 'Path' because it is null.
At C:\Users\johndoe\Desktop\removeprofile2.ps1:37 char:38
+             Remove-Recursively -path $pfolder
+                                      ~~~~~~~~
    + CategoryInfo          : InvalidData: (:) [Remove-Recursively], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Remove-Recursively
 
Remove-Recursively : Cannot bind argument to parameter 'Path' because it is null.
At C:\Users\johndoe\Desktop\removeprofile2.ps1:37 char:38
+             Remove-Recursively -path $pfolder
+                                      ~~~~~~~~
    + CategoryInfo          : InvalidData: (:) [Remove-Recursively], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Remove-Recursively
 
Remove-Recursively : Cannot bind argument to parameter 'Path' because it is null.
At C:\Users\johndoe\Desktop\removeprofile2.ps1:37 char:38
+             Remove-Recursively -path $pfolder
+                                      ~~~~~~~~
    + CategoryInfo          : InvalidData: (:) [Remove-Recursively], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Remove-Recursively

Open in new window


@shaun, GPO would it sounded good, thought this project is a temporary ...
0
Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

ivan rosaAuthor Commented:
ok so i re-edited the script with vision of just deleting the entire profile while skipping the default accounts but for some reason it's not deleting the files ...
$CLIENT = Get-Content "c:\Temp\computers.txt"

foreach($PC in $CLIENT){
    # Testing PC connectivity
    if(Test-Connection $PC -Count 1 -Quiet){
        # Gathering users from c:\users
        foreach($PCuser in (Get-ChildItem "\\$pc\c$\users" -Directory)){
            # delete  recursively
            Where ($PCuser -ne 'Default' -or $PCuser -ne 'Administrator' -ne $PCuser -ne 'Public') | RD "\\$PC\c$\Users\$PCuser\" -Force -Recurse
            echo "from this $PC all of this user was deleted $PCuser"
        }
    }else{
        Write-Warning "$PC is offline"
    }
}

Open in new window

can anybody kindly take a look of what am i doing wrong?
0
Jose Gabriel Ortega CastroCEOCommented:
$CLIENT = Get-Content "c:\Temp\computers.txt"
function Remove-Recursively{
    [CmdletBinding()]
    param(
        [Parameter(position=0,Mandatory=$true)]$Path
    )
    BEGIN{}
    PROCESS{
        #Get Files and remove them
        $Files = [System.IO.Directory]::GetFiles("$Path")
        $ProfileFolders = [System.IO.Directory]::GetDirectories("$Path")
        if($ProfileFolders.Count -gt 0){
            foreach($profileFolder in $ProfileFolders){
                Remove-recursively -path "$profilefolder"
            }
        }
        

        if($Files.Count -gt 0){
            foreach($file in $Files){
                [System.IO.File]::Delete($file)
            }
        }
        [System.IO.Directory]::Delete("$Path")
    }
    END{}
}
 
foreach($PC in $CLIENT){
    # Testing PC connectivity
    if(Test-Connection $PC -Count 1 -Quiet){
        # Gathering users from c:\users
        $Folders= [System.IO.Directory]::GetDirectories("\\$pc\C$\users")
        $Filtered = $Folders | where { $_ -notmatch "Admin"}

        foreach($PCuser in $Filtered){
            Remove-Recursively -path "$pfolder"
        }
    }else{
        Write-Warning "$PC is offline"
    }
} 

Open in new window


Try this one and check if you got any errors.
it was not working because you gave a null path.
That was all it was complaining for.
0
ivan rosaAuthor Commented:
thanks, buddy, although I got me an even bigger error log.

Exception calling "GetFiles" with "1" argument(s): "The path is not of a legal form."
At C:\Temp\removeprofiles.ps1:10 char:9
+         $Files = [System.IO.Directory]::GetFiles("$Path")
+         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : ArgumentException

Exception calling "GetDirectories" with "1" argument(s): "The path is not of a legal form."
At C:\Temp\removeprofiles.ps1:11 char:9
+         $ProfileFolders = [System.IO.Directory]::GetDirectories("$Pat ...
+         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : ArgumentException

Exception calling "Delete" with "1" argument(s): "The path is not of a legal form."
At C:\Temp\removeprofiles.ps1:24 char:9
+         [System.IO.Directory]::Delete("$Path")
+         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : ArgumentException

Exception calling "GetFiles" with "1" argument(s): "The path is not of a legal form."
At C:\Temp\removeprofiles.ps1:10 char:9
+         $Files = [System.IO.Directory]::GetFiles("$Path")
+         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : ArgumentException

Exception calling "GetDirectories" with "1" argument(s): "The path is not of a legal form."
At C:\Temp\removeprofiles.ps1:11 char:9
+         $ProfileFolders = [System.IO.Directory]::GetDirectories("$Pat ...
+         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : ArgumentException

Exception calling "Delete" with "1" argument(s): "The path is not of a legal form."
At C:\Temp\removeprofiles.ps1:24 char:9
+         [System.IO.Directory]::Delete("$Path")
+         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : ArgumentException

Exception calling "GetFiles" with "1" argument(s): "The path is not of a legal form."
At C:\Temp\removeprofiles.ps1:10 char:9
+         $Files = [System.IO.Directory]::GetFiles("$Path")
+         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : ArgumentException

Exception calling "GetDirectories" with "1" argument(s): "The path is not of a legal form."
At C:\Temp\removeprofiles.ps1:11 char:9
+         $ProfileFolders = [System.IO.Directory]::GetDirectories("$Pat ...
+         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : ArgumentException

Exception calling "Delete" with "1" argument(s): "The path is not of a legal form."
At C:\Temp\removeprofiles.ps1:24 char:9
+         [System.IO.Directory]::Delete("$Path")
+         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : ArgumentException

Exception calling "GetFiles" with "1" argument(s): "The path is not of a legal form."
At C:\Temp\removeprofiles.ps1:10 char:9
+         $Files = [System.IO.Directory]::GetFiles("$Path")
+         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : ArgumentException

Exception calling "GetDirectories" with "1" argument(s): "The path is not of a legal form."
At C:\Temp\removeprofiles.ps1:11 char:9
+         $ProfileFolders = [System.IO.Directory]::GetDirectories("$Pat ...
+         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : ArgumentException

Exception calling "Delete" with "1" argument(s): "The path is not of a legal form."
At C:\Temp\removeprofiles.ps1:24 char:9
+         [System.IO.Directory]::Delete("$Path")
+         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : ArgumentException

Exception calling "GetFiles" with "1" argument(s): "The path is not of a legal form."
At C:\Temp\removeprofiles.ps1:10 char:9
+         $Files = [System.IO.Directory]::GetFiles("$Path")
+         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : ArgumentException

Exception calling "GetDirectories" with "1" argument(s): "The path is not of a legal form."
At C:\Temp\removeprofiles.ps1:11 char:9
+         $ProfileFolders = [System.IO.Directory]::GetDirectories("$Pat ...
+         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : ArgumentException

Exception calling "Delete" with "1" argument(s): "The path is not of a legal form."
At C:\Temp\removeprofiles.ps1:24 char:9
+         [System.IO.Directory]::Delete("$Path")
+         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : ArgumentException

Exception calling "GetFiles" with "1" argument(s): "The path is not of a legal form."
At C:\Temp\removeprofiles.ps1:10 char:9
+         $Files = [System.IO.Directory]::GetFiles("$Path")
+         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : ArgumentException

Exception calling "GetDirectories" with "1" argument(s): "The path is not of a legal form."
At C:\Temp\removeprofiles.ps1:11 char:9
+         $ProfileFolders = [System.IO.Directory]::GetDirectories("$Pat ...
+         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : ArgumentException

Exception calling "Delete" with "1" argument(s): "The path is not of a legal form."
At C:\Temp\removeprofiles.ps1:24 char:9
+         [System.IO.Directory]::Delete("$Path")
+         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : ArgumentException

Exception calling "GetFiles" with "1" argument(s): "The path is not of a legal form."
At C:\Temp\removeprofiles.ps1:10 char:9
+         $Files = [System.IO.Directory]::GetFiles("$Path")
+         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : ArgumentException

Exception calling "GetDirectories" with "1" argument(s): "The path is not of a legal form."
At C:\Temp\removeprofiles.ps1:11 char:9
+         $ProfileFolders = [System.IO.Directory]::GetDirectories("$Pat ...
+         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : ArgumentException

Exception calling "Delete" with "1" argument(s): "The path is not of a legal form."
At C:\Temp\removeprofiles.ps1:24 char:9
+         [System.IO.Directory]::Delete("$Path")
+         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : ArgumentException

Open in new window

0
Jose Gabriel Ortega CastroCEOCommented:
I'd need to know what is in the variable. Can I see it ? teamviewer maybe?
what is the path you're looking form? it is supposed to run in a local path. are you running it in a network path (\\server\folder) ?
0
ivan rosaAuthor Commented:
thanks for your efforts Jose I was actually running it from the same "c:\test\" folder just like where my computers.txt file was...
with that said though I corrected my original one and actually got it working. I even was able to add more users to avoid.
ps. it might not be a look a little messy though

$CLIENT = Get-Content "c:\Temp\computers.txt"
foreach($PC in $CLIENT) 
{
    if(Test-Connection $PC -Count 1 -Quiet) 
    {
        foreach($PCuser in (Get-ChildItem "\\$pc\c$\users" -Directory)) 
        {
            if ($PCuser -notmatch "Public") 
            {
                if ($PCuser -notmatch "Administrator") 
                {
                    if ($PCuser -notmatch "Default")
                    {
                        #WRITE This is the user $PCuser - and pc $PC
                        Remove-Item -Recurse -Force -Verbose -Path "\\$PC\C$\Users\$PCuser\"
                    }
                    
                } 
            }
    
        }
    } else
        {
            Write-Warning "$PC is offline"
        }
}
 

Open in new window

1

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Shaun VermaakTechnical SpecialistCommented:
You will still delete admin profiles because admin could have been renamed or suffixed with a .COMPUTERNAME or a .DOMAINNAME.

I suggest you enumerate HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList
0
ivan rosaAuthor Commented:
this is best to put in practice with folders within folders while voiding targets. thought i would really advise to test it first make sure if it's used for profiling to make sure, Temporary profile doesn't pop up...
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.