Skip to content

Instantly share code, notes, and snippets.

@Goadstir
Created January 8, 2020 20:39
Show Gist options
  • Select an option

  • Save Goadstir/609d11ba5d553675fc9a5458161b0c7e to your computer and use it in GitHub Desktop.

Select an option

Save Goadstir/609d11ba5d553675fc9a5458161b0c7e to your computer and use it in GitHub Desktop.
PowerShell: Create Nicely Formatted Output
<#
References:
* http://powershell-guru.com/dont-13-use-write-host-properly-align-output/
#>
# Solution 1
[PSCustomObject] @{
Date = Get-Date -Format d
Computer = [System.Environment]::MachineName
Username = ([Security.Principal.WindowsIdentity]::GetCurrent()).Name
} | Format-List
# Solution 2
$properties = @{
Date = Get-Date -Format d
Computer = [System.Environment]::MachineName
Username = ([Security.Principal.WindowsIdentity]::GetCurrent()).Name
}
New-Object -TypeName PSCustomObject -Property $properties | Format-List
# Solution 3
New-Object -TypeName PSObject -Property @{
Date = Get-Date -Format d
Computer = [System.Environment]::MachineName
Username = ([Security.Principal.WindowsIdentity]::GetCurrent()).Name
} | Format-List
# Solution 4
$date = Get-Date -Format d
$computer = [System.Environment]::MachineName
$username = ([Security.Principal.WindowsIdentity]::GetCurrent()).Name
$result = New-Object -TypeName PSObject
$result | Add-Member -MemberType Noteproperty -Name Date -Value $($date)
$result | Add-Member -MemberType Noteproperty -Name Computer -Value $($computer)
$result | Add-Member -MemberType Noteproperty -Name Username -Value $($username)
$result | Format-List
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment