$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"
}
}
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
$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"
}
}
can anybody kindly take a look of what am i doing wrong?
$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"
}
}
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