Don’t do that: The following code needs to properly align the output of these 3 variables:
1 2 3 4 5 6 7 |
$date = Get-Date -Format d $computer = [System.Environment]::MachineName $username = ([Security.Principal.WindowsIdentity]::GetCurrent()).Name Write-Host "Date : $date" Write-Host "Computer : $computer" Write-Host "Username : $username" |
Do that: A better way to output objects:
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 |
# 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 |