Reputation: 35
I'm getting two different results when using the same command depending on how it is used. I'm using PowerShell version 5.
Typing the following into the console, as expected I get a short list of commands available in the PSReadline module.
gcm -module psreadline
However when I attempt the same using the script below I get a very long list of TMI.
The script simply lists all loaded modules and then applies the same command as above however this time it is applied via input from the user that specifies a module name.
Can anyone make the script below just output the brief list of commands as the above command does?
The module used to test this can be another module - doesn't have to be psreadline.
Thanks in advance.
# List loaded modules & get commands for a module specified by the user via user input:
cls
write-host "`n`n`n"
write-host " Loaded Modules: " -f darkblue -b white
write-host "`n`n"
get-module
write-host "`n`n"
$strString = " Get commands for a module "
write-host $strString -f darkblue -b white
write-host "`n`n`n"
$input=Read-Host " Enter module name: " ;
gcm -module $input
Upvotes: 2
Views: 132
Reputation: 174690
When an object is written to the console, PowerShell first tries to apply type-based formatting - you can read more about this topic with Get-Help about_Format.ps1xml
.
When more than one object is written, formatting is only applied for one type - usually the first non-primitive type - any additional objects of another type will be piped through Format-List
, which is why you see way more output than you expect.
Consider the following example:
PS C:\> @(Get-Service)[0],@(Get-Process)[0]
Status Name DisplayName
------ ---- -----------
Stopped AService A Service
Id : 2816
Handles : 264
CPU : 1.078125
Name : ApplicationFrameHost
Automatic formatting is applied to the ouput from Get-Service
, but not Get-Process
However, if we re-order them:
PS C:\> @(Get-Process)[0],@(Get-Service)[0]
Handles NPM(K) PM(K) WS(K) VM(M) CPU(s) Id ProcessName
------- ------ ----- ----- ----- ------ -- -----------
264 22 17548 20988 ...25 1.08 2816 ApplicationFrameHost
Status : Stopped
Name : AService
DisplayName : A Service
Get-Process
is now "first out the door" and formatting is applied to that output type, but not subsequent objects of another distinct type
Since you've already called Get-Module
prior to Get-Command
, the above applies in your case.
You can control output formatting yourself by piping to a Format-*
cmdlet:
Get-Module | Format-Table Name,Version
Get-Command -Module PSReadLine | Format-Table Name,CommandType
Upvotes: 3