Get-UserProfile.psm1
function Get-UserProfile { <# .SYNOPSIS The Get-UserProfile module list or remove User Profiles from local or remote computer. List with Active Directory SearchBase. .DESCRIPTION The Get-UserProfile module list or remove User Profiles from local or remote computer. List with Active Directory SearchBase. .PARAMETER ComputerName Name of computer to list User Profiles from local or remote computer. If use Parameter ComputerName, than Parameter ADSearchBase is disable. .PARAMETER ADSearchBase Active Directory SearchBase of computer to list User Profiles from local or remote computer. If use Parameter ADSearchBase, than Parameter ComputerName is disable. .PARAMETER LocalPath Filter for colum LocalPath. .PARAMETER RemoveForce Remove User Profiles from local or remote computer without Confirm. .EXAMPLE Get-UserProfile List User Profiles from local computer. .EXAMPLE Get-UserProfile -ComputerName pc1, pc2 List User Profiles on multiple remote computer. .EXAMPLE Get-UserProfile -SearchBase "OU=Computers,DC=comodo,DC=com". List User Profiles on Active Directory SearchBase computer. .EXAMPLE Get-UserProfile -ComputerName pc1, pc2 -Remove -RemoveForce List User Profiles on multiple remote computer an remove it without confirm. .EXAMPLE Get-UserProfile -ComputerName pc1, pc2 -LocalPath "test1" List User Profile with wildcard filter for LocalPath on multiple remote computer. #> [CmdletBinding(DefaultParameterSetName = "Par1")] param ( [parameter(Position = 0, ParameterSetName = "Par1")] [string[]] $ComputerName = "localhost", [parameter(ParameterSetName = "Par2")] [string] $ADSearchBase, [parameter(ParameterSetName = "Par1")] [parameter(ParameterSetName = "Par2")] [string] $LocalPath, [parameter(ParameterSetName = "Par1")] [parameter(ParameterSetName = "Par2")] [switch] $RemoveForce ) if ($PsVersionTable.OS -match "Windows") { if ($ADSearchBase) { try { $ADSearch = New-Object System.DirectoryServices.DirectorySearcher $ADSearch.SearchRoot = New-Object System.DirectoryServices.DirectoryEntry("LDAP://$ADSearchBase") $ADSearch.Filter = "(&(objectClass=computer)(objectCategory=computer)(!useraccountcontrol:1.2.840.113556.1.4.803:=2))" $ADSearchFind = $ADSearch.FindAll() } catch { Write-Warning "ADSearchBase not found" } $ComputerName = $ADSearchFind | ForEach-Object -Parallel { $_.Properties.name } | Sort-Object } if ($ComputerName) { $UserProfileObject = $ComputerName | ForEach-Object -Parallel { $Computer = $_.ToUpper() if (($Computer -like "localhost") -or ((Test-Connection -Count 1 -TargetName $Computer).Status -match "Success")) { $UserProfile = Get-CimInstance Win32_UserProfile -ComputerName $Computer -Filter "Special = 'False'" if (($UserProfile).Count -gt 1) { foreach ($UserProfileItem in $UserProfile) { try { $UserName = (New-Object System.Security.Principal.SecurityIdentifier($UserProfileItem.SID)).Translate([System.Security.Principal.NTAccount]).Value } catch { $UserName = $null } [PSCustomObject]@{ PSTypeName = "Get-UserProfile" ComputerName = $UserProfileItem.PSComputerName LastUseTime = $UserProfileItem.LastUseTime LocalPath = $UserProfileItem.LocalPath Loaded = $UserProfileItem.Loaded Roaming = $UserProfileItem.RoamingConfigured UserName = $UserName SID = $UserProfileItem.SID } } } } } if ($LocalPath) { $Output = $UserProfileObject | Where-Object { $_.LocalPath -like $LocalPath } } else { $Output = $UserProfileObject } if ($RemoveForce) { $Output | ForEach-Object -Parallel { $ComputerName = $_.ComputerName $Loaded = $_.Loaded $SID = $_.SID if ($Loaded -eq "True") { Write-Warning "$SID Loaded" } else { Get-CimInstance Win32_UserProfile -ComputerName $ComputerName -Filter "SID = '$SID'" | Remove-CimInstance -Verbose } } } else { $Output } } } else { Write-Warning "PSVersion OS not Windows" } } New-Alias -Name gup -Value Get-UserProfile Export-ModuleMember -Alias * -Function Get-UserProfile |