Created
January 8, 2020 20:39
-
-
Save Goadstir/609d11ba5d553675fc9a5458161b0c7e to your computer and use it in GitHub Desktop.
PowerShell: Create Nicely Formatted Output
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <# | |
| 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