I am attempting to run the following code to retrieve a list of local users on a machine.
gwmi win32_useraccount -Computername $env:computername -Filter "Domain='$env:computername'" |
Format-Table Name,Description
I get this error when running inside a PS1 file:
The object of type "Microsoft.PowerShell.Commands.Internal.Format.FormatStartData" is not valid or not in the correct sequence. This is likely caused by a user-specified "f ormat-table" command which is conflicting with the default formatting. + CategoryInfo : InvalidData: (:) [out-lineoutput], InvalidOperationException + FullyQualifiedErrorId : ConsoleLineOutputOutOfSequencePacket,Microsoft.PowerShell.Commands.OutLineOutputCommand
I understand this issue arises because of the way the pipelines are parsed but I can't figure out how to get around it.
The Format-Table cmdlet formats the output of a command as a table with the selected properties of the object in each column. The object type determines the default layout and properties that are displayed in each column. You can use the Property parameter to select the properties that you want to display.
What to do if you want to clean out all the entries in $error? $error is a variable, so you can try with the Clear-Variable cmdlet: PS> Clear-Variable error -Force Clear-Variable : Cannot overwrite variable Error because it is read-only or constant.
You can use the ErrorAction parameter on every cmdlet and advanced function in PowerShell. It is a great way to choose which commands should stop script execution and which ones should not.
The PowerShell ErrorAction parameter allows handling the actions if any error occurs. By default, PowerShell operates on the continue option for error handling. However, the ErrorAction operator can be used to change the default option.
The Format-*
cmdlets do not do final output, but transform their input into a sequence of formatting objects. These formatting objects are converted to the actual output by one of the Out-
cmdlets, probably Out-Default
.
If a script has multiple, different, sets of formatting objects that final output of the merged objects from all the expressions in the script Out-Default
cannot resolve the inconsistencies.
Fix: add a Out-Sting
to the end of each output generating pipeline to perform the formatting one expression at a time:
gwmi win32_useraccount -Computername $env:computername -Filter "Domain='$env:computername'" |
Format-Table Name,Description | Out-String
you can also try :
gwmi win32_useraccount -Computername $env:computername -Filter "Domain='$env:computername'" | Select-Object Name,Description | Format-Table Name,Description
In fact you convert to an intermediate PSCustomObject
and you still have an object.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With