MyITGuy
8/13/2016 - 1:09 PM

PowerShell: Current User / Domain / ComputerName

PowerShell: Current User / Domain / ComputerName

[Environment]::UserName
[Environment]::UserDomainName
[Environment]::MachineName

[System.Security.Principal.WindowsIdentity]::GetCurrent().Name

$env:UserName

Write-Host (& "$($env:windir)\system32\whoami.exe")

Get-CimInstance -ClassName Win32_ComputerSystem | Select-Object -ExpandProperty username

#Set PowerShell Window Title
$host.UI.RawUI.WindowTitle = [System.Security.Principal.WindowsIdentity]::GetCurrent().Name
function Get-ADSICurrentUser {
    #region ConvertFrom-ADSIObject
    function ConvertFrom-ADSIObject {
        [CmdletBinding()]
        PARAM(
            [Parameter(Mandatory = $true, Position = 0, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)]
            [object]
            $InputObject
        )

        begin {
            Write-Verbose $MyInvocation.MyCommand
        }

        process {
            Write-Verbose $InputObject.GetType().FullName
            switch ($true) {
            ($InputObject -is [System.DirectoryServices.SearchResult]) {
                    $SearchResultCollection = @($InputObject)
                    break
                }
            ($InputObject -is [System.DirectoryServices.SearchResultCollection]) {
                    $SearchResultCollection = $InputObject
                    break
                }
            ($InputObject -is [System.DirectoryServices.DirectoryEntry]) {
                    $SearchResultCollection = @($InputObject)
                    break
                }
                default {
                    Throw "Unsupported object."
                }
            }
            try {
                $Properties = @{}
                foreach ($SearchResult in $SearchResultCollection) {
                    foreach ($PropertyName In $SearchResult.Properties.PropertyNames) {
                        $PropertyValue = $SearchResult.Properties[$PropertyName]
                        if ($PropertyValue.Count -gt 1) {
                            $PropertyValue = $PropertyValue | ForEach-Object { $_ }
                        }
                        else {
                            $PropertyValue = $PropertyValue[0]
                        }
                        $Properties."$([string]$PropertyName)" = $PropertyValue
                    }
                    [PSCustomObject]$Properties
                }
            }
            catch {
                Throw $_
            }
        }

        end {
        }
    }
    Set-Alias -Name ConvertFrom-SearchResult -Value ConvertFrom-ADSIObject
    Set-Alias -Name ConvertFrom-SearchResultCollection -Value ConvertFrom-ADSIObject
    Set-Alias -Name ConvertFrom-DirectoryEntry -Value ConvertFrom-ADSIObject
    #endregion ConvertFrom-ADSIObject
    $IADsUser = [adsi]"LDAP://$(& "$($env:windir)\system32\whoami.exe" /fqdn)"

    $IADsUser | ConvertFrom-ADSIObject
}
Start-Process -FilePath 'cmd.exe' -ArgumentList "/c `"$($env:LOGONSERVER)\NETLOGON\$($ADSICurrentUser.scriptPath)`""