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 SID
    Filter for colum SID.
.PARAMETER Loaded
    False or True filter for colum Loaded.
.PARAMETER Roaming
    False or True filter for colum Roaming.
.PARAMETER Special
    All or True filter for colum Roaming. Default is False.
.PARAMETER Days
    Older than x days filter.
.PARAMETER Remove
    Remove User Profiles from local or remote computer. Default with Confirm.
    To disable the SwitchParameter Confirm use Parameter Force.
.PARAMETER Force
    Disable the built-in Format-Table, Sort-Object and SwitchParameter Confirm from Parameter Remove.
.EXAMPLE
    Get-UserProfile
    List User Profiles from local computer with built-in Format-Table and Sort-Object.
    ComputerName LastUseTime LocalPath Loaded Roaming Special UserName SID
    ------------ ----------- --------- ------ ------- ------- -------- ---
                                     C:\Users\test1 False False False NB01\test1 S-1-5-21-913119323-3770826481-2848448841-1005
                 27.09.2018 10:25:56 C:\Users\test2 True False False NB01\test2 S-1-5-21-913119323-3770826481-2848448841-1002
.EXAMPLE
    Get-UserProfile -ComputerName pc1,pc2
    List User Profiles on multiple remote computer with built-in Format-Table and Sort-Object.
.EXAMPLE
    Get-UserProfile -SearchBase "OU=Computers,DC=comodo,DC=com" with built-in Format-Table and Sort-Object.
    List User Profiles on Active Directory SearchBase computer.
.EXAMPLE
    Get-UserProfile -ComputerName pc1,pc2 -Force
    List User Profiles on multiple remote computer and disable the built-in Format-Table, Sort-Object and disable the Confirm Switch from Parameter Remove.
.EXAMPLE
    Get-UserProfile -ComputerName pc1,pc2 -LocalPath "test1"
    List User Profile with wildcard filter for LocalPath on multiple remote computer.
.EXAMPLE
    Get-UserProfile -LocalPath "test1" -Remove
    Remove User Profiles with wildcard filter for LocalPath from local computer.
.EXAMPLE
    Get-UserProfile -ComputerName pc1,pc2 -SID "S-1-5-21-1409082233-651377327-725345643-2820" -Remove -Force
    Remove User Profile with specific filter for SID on multiple remote computer
    Disable the built-in Format-Table, Sort-Object and SwitchParameter Confirm from Parameter Remove.
.EXAMPLE
    Get-UserProfile -ComputerName pc1,pc2 -Days 30 -Remove -Force
    Remove User Profiles with older than x days filter on multiple remote computer.
    Disable the built-in Format-Table, Sort-Object and SwitchParameter Confirm from Parameter Remove.
#>

    [CmdletBinding(DefaultParameterSetName = "ParS1")]
    param (
        [parameter(Position = 0, ParameterSetName = "ParS1", Mandatory = $false, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)]
        [string[]] $ComputerName,
        [parameter(ParameterSetName = "ParS2", Mandatory = $false, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)]
        [string] $ADSearchBase,
        [parameter(ParameterSetName = "ParS1", Mandatory = $false, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)]
        [parameter(ParameterSetName = "ParS2", Mandatory = $false, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)]
        [string] $LocalPath,
        [parameter(ParameterSetName = "ParS1", Mandatory = $false, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)]
        [parameter(ParameterSetName = "ParS2", Mandatory = $false, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)]
        [string] $SID,
        [parameter(ParameterSetName = "ParS1", Mandatory = $false, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)]
        [parameter(ParameterSetName = "ParS2", Mandatory = $false, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)]
        [ValidateSet("False", "True")]
        [string] $Loaded,
        [parameter(ParameterSetName = "ParS1", Mandatory = $false, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)]
        [parameter(ParameterSetName = "ParS2", Mandatory = $false, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)]
        [ValidateSet("False", "True")]
        [string] $Roaming,
        [parameter(ParameterSetName = "ParS1", Mandatory = $false, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)]
        [parameter(ParameterSetName = "ParS2", Mandatory = $false, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)]
        [ValidateSet("All", "True")]
        [string] $Special,
        [parameter(ParameterSetName = "ParS1", Mandatory = $false, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)]
        [parameter(ParameterSetName = "ParS2", Mandatory = $false, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)]
        [int] $Days,
        [parameter(ParameterSetName = "ParS1", Mandatory = $false, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)]
        [parameter(ParameterSetName = "ParS2", Mandatory = $false, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)]
        [switch] $Remove,
        [parameter(ParameterSetName = "ParS1", Mandatory = $false, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)]
        [parameter(ParameterSetName = "ParS2", Mandatory = $false, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)]
        [switch] $Force
    )
    # LocalPath
    if ($LocalPath) {
        $LocalPathR = $LocalPath.Replace("*", "%")
        $Filter1 = "LocalPath like '$LocalPathR'"
    }
    else {
        $Filter1 = "LocalPath like '%'"
    }
    # SID
    if ($SID) {
        $Filter2 = "and SID ='$SID'"
    }
    else {
        $Filter2 = $null
    }
    # Loaded
    if ($Loaded) {
        $Filter3 = "and Loaded = '$Loaded'"
    }
    else {
        $Filter3 = $null
    }
    # Roaming
    if ($Roaming) {
        $Filter4 = "and RoamingConfigured = '$Roaming'"
    }
    else {
        $Filter4 = $null
    }
    # Special
    if ($Special -eq "All") {
        $Filter5 = $null
    }
    elseif ($Special -eq "True") {
        $Filter5 = "and Special = 'True'"
    }
    else {
        $Filter5 = "and Special = 'False'"
    }
    # Days
    if ($Days) {
        $AddDays = [System.Management.ManagementDateTimeConverter]::ToDmtfDateTime((Get-Date).AddDays((-$Days)))
        $Filter6 = "and LastUseTime<='$AddDays'"
    }
    else {
        $Filter6 = $null
    }
    # Filters
    $Filters = "$Filter1 $Filter2 $Filter3 $Filter4 $Filter5 $Filter6"
    # SearchBase
    if ($ADSearchBase) {
        if (Get-Command Get-AD* -ErrorAction SilentlyContinue) {
            if (Get-ADOrganizationalUnit -Filter "distinguishedName -eq '$ADSearchBase'" -ErrorAction SilentlyContinue) {
                $ComputerName = (Get-ADComputer -SearchBase $ADSearchBase -Filter *).Name
            }
            else {
                Write-Warning "No Active Directory SearchBase found"
            }
        }
        else {
            Write-Warning "No Active Directory Cmdlets found"
        }
    }
    # ComputerName Remote
    if ($ComputerName) {
        $Comp1 = New-TemporaryFile
        foreach ($Comp2 in $ComputerName) {
            if (Test-Connection -ComputerName $Comp2 -Count 1 -ErrorAction SilentlyContinue) {
                $Comp2 | Out-File -FilePath $Comp1 -Append
            }
            else {
                Write-Warning "$Comp2 not reachable"
            }
        }
        $UserP1 = Get-CimInstance Win32_UserProfile -ComputerName (Get-Content $Comp1) -Filter $Filters
    }
    # ComputerName Local
    if (!$ComputerName -and !$ADSearchBase) {
        $UserP1 = Get-CimInstance Win32_UserProfile -Filter $Filters
    }
    # Force
    if ($Force) {
        $UserP1 # Force Output
        if ($Remove) {
            if ($UserP1.Loaded -eq "True") {
                Write-Warning "Remove Loaded True"
            }
            else {
                $UserP1 | Remove-CimInstance -Verbose
            }
        }
    }
    else {
        $UserP1 | Sort-Object -Property PSComputerName, LocalPath | Format-Table -AutoSize -Property @{LABEL = "ComputerName"; EXPRESSION = {$_.PSComputerName}}, LastUseTime, LocalPath, Loaded, @{LABEL = "Roaming"; EXPRESSION = {$_.RoamingConfigured}}, Special, @{LABEL = "UserName"; EXPRESSION = {(New-Object System.Security.Principal.SecurityIdentifier($_.Sid)).Translate([System.Security.Principal.NTAccount]).Value}}, SID
        if ($Remove) {
            if ($UserP1.Loaded -eq "True") {
                Write-Warning "Remove Loaded True"
            }
            else {
                $UserP1 | Remove-CimInstance -Verbose -Confirm
            }
        }
    }
}