Get-UserProfile.psm1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
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