Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > PowerShell

Vista - commas in PowerShell

Reply
 
Old 07-11-2009   #1 (permalink)
Larry__Weiss


 
 

commas in PowerShell

This works
get-help about* | Format-Table -Property Name, Synopsis

This does not work
get-help about* | Format-Table -Property Name Synopsis

What is the significance of that comma that makes the first version meaningful?

- Larry

My System SpecsSystem Spec
Old 07-11-2009   #2 (permalink)
Vadims Podans [MVP]


 
 

Re: commas in PowerShell

get-help about* | Format-Table -Property Name, Synopsis
this works because Name and Synopsis are passed as array to -Property
parameter.

get-help about* | Format-Table -Property Name Synopsis
this doesn't work, because space is delimiter between parameters. Therefore
PowerShell tries to bind "Synopsis" to the next positional paramter and
uneble to find. The same behaviour is with passing arguments to functions.
If you pass arguments separating them with coma - they will passed as array
and single argument. If you separate arguments with spaces - each parameter
will be passed as different argument.
--
WBR, Vadims Podans
MVP: PowerShell
PowerShell blog - www.sysadmins.lv

"Larry__Weiss" <lfw@xxxxxx> rakstija zinojuma
"news:ukPAzvlAKHA.1380@xxxxxx"...
Quote:

> This works
> get-help about* | Format-Table -Property Name, Synopsis
>
> This does not work
> get-help about* | Format-Table -Property Name Synopsis
>
> What is the significance of that comma that makes the first version
> meaningful?
>
> - Larry
My System SpecsSystem Spec
Old 07-11-2009   #3 (permalink)
Joel Bennett


 
 

Re: commas in PowerShell

Yeah, in short: commas make arrays, just like @() does. It works on
parameters which take arrays, or on assignments:

$foo = 1,2,3 # foo is an array of objects containing three numbers
$foo = @(1,2,3) # foo is an array of objects containing three numbers
$foo = ,1 # foo is an array of objects containing a single number
$foo = @(1) # foo is an array of objects containing a single number

In each case $foo.GetType() will return Object[] (an array of Objects).
The only way to avoid getting an object array is to cast it specifically.



Vadims Podans [MVP] wrote:
Quote:

> get-help about* | Format-Table -Property Name, Synopsis
> this works because Name and Synopsis are passed as array to -Property
> parameter.
>
> get-help about* | Format-Table -Property Name Synopsis
> this doesn't work, because space is delimiter between parameters.
> Therefore PowerShell tries to bind "Synopsis" to the next positional
> paramter and uneble to find. The same behaviour is with passing
> arguments to functions. If you pass arguments separating them with coma
> - they will passed as array and single argument. If you separate
> arguments with spaces - each parameter will be passed as different
> argument.
My System SpecsSystem Spec
Old 07-11-2009   #4 (permalink)
Larry__Weiss


 
 

Re: commas in PowerShell

Thanks!
I thought about it looking a lot like an array specification,
and tried different array syntax to try to confirm it.
For example,
get-help about* | Format-Table -Property @(Name, Synopsis)
but that would not work.

So with your confirmation that we are passing an array I now find that
get-help about* | Format-Table -Property @("Name", "Synopsis")
works fine (and takes any mystery out of it for me!)

As an aside,
I wonder if anyone has written a translator for the usual PowerShell
diagnostic verbiage? I don't know if I'll ever become fluent in that lingo.
I think that perhaps a more terse version would be less overwhelming.

For example for
get-help about* | Format-Table -Property @(Name, Synopsis)
I get

<#
The term 'Name' is not recognized as a cmdlet, function, operable program, or
script file. Verify t
he term and try again.
At line:1 char:48
+ get-help about* | Format-Table -Property @(Name <<<< , Synopsis)
+ CategoryInfo : ObjectNotFound: (Name:String) [],
CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
Format-Table : Object reference not set to an instance of an object.
At line:1 char:31
+ get-help about* | Format-Table <<<< -Property @(Name, Synopsis)
+ CategoryInfo : NotSpecified: ( [Format-Table],
NullReferenceException
+ FullyQualifiedErrorId :
System.NullReferenceException,Microsoft.PowerShell.Commands.FormatTa
bleCommand
#>

(At least it wraps words without "..." truncation.)

- Larry

Vadims Podans [MVP] wrote:
Quote:

> get-help about* | Format-Table -Property Name, Synopsis
> this works because Name and Synopsis are passed as array to -Property
> parameter.
>
> get-help about* | Format-Table -Property Name Synopsis
> this doesn't work, because space is delimiter between parameters.
> Therefore PowerShell tries to bind "Synopsis" to the next positional
> paramter and uneble to find. The same behaviour is with passing
> arguments to functions. If you pass arguments separating them with coma
> - they will passed as array and single argument. If you separate
> arguments with spaces - each parameter will be passed as different
> argument.
My System SpecsSystem Spec
Old 07-11-2009   #5 (permalink)
Vadims Podans [MVP]


 
 

Re: commas in PowerShell

if you use @() notation then you must place inside parameters in quotes. In
first PS parser will try to convert to array object as in console and after
this object will be passed as arguments:

[↓] [vPodans] @("name", "synopsis")
name
synopsis
[↓] [vPodans] @(name, synopsis)
The term 'name' is not recognized as a cmdlet, function, operable program,
or script file. Verify the term and try agai
n.
At line:1 char:7
+ @(name <<<< , synopsis)
+ CategoryInfo : ObjectNotFound: (name:String) [],
CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException

[↓] [vPodans]

first example is identical as Get-Help -property Name, Synopsis because it
already accept arrays (as mentioned Joel).
--
WBR, Vadims Podans
MVP: PowerShell
PowerShell blog - www.sysadmins.lv

"Larry__Weiss" <lfw@xxxxxx> rakstīja ziņojumā
"news:#nsAGemAKHA.2604@xxxxxx"...
Quote:

> Thanks!
> I thought about it looking a lot like an array specification,
> and tried different array syntax to try to confirm it.
> For example,
> get-help about* | Format-Table -Property @(Name, Synopsis)
> but that would not work.
>
> So with your confirmation that we are passing an array I now find that
> get-help about* | Format-Table -Property @("Name", "Synopsis")
> works fine (and takes any mystery out of it for me!)
>
> As an aside,
> I wonder if anyone has written a translator for the usual PowerShell
> diagnostic verbiage? I don't know if I'll ever become fluent in that
> lingo.
> I think that perhaps a more terse version would be less overwhelming.
>
> For example for
> get-help about* | Format-Table -Property @(Name, Synopsis)
> I get
>
> <#
> The term 'Name' is not recognized as a cmdlet, function, operable program,
> or script file. Verify t
> he term and try again.
> At line:1 char:48
> + get-help about* | Format-Table -Property @(Name <<<< , Synopsis)
> + CategoryInfo : ObjectNotFound: (Name:String) [],
> CommandNotFoundException
> + FullyQualifiedErrorId : CommandNotFoundException
> Format-Table : Object reference not set to an instance of an object.
> At line:1 char:31
> + get-help about* | Format-Table <<<< -Property @(Name, Synopsis)
> + CategoryInfo : NotSpecified: ( [Format-Table],
> NullReferenceException
> + FullyQualifiedErrorId :
> System.NullReferenceException,Microsoft.PowerShell.Commands.FormatTa
> bleCommand
> #>
>
> (At least it wraps words without "..." truncation.)
>
> - Larry
>
> Vadims Podans [MVP] wrote:
Quote:

>> get-help about* | Format-Table -Property Name, Synopsis
>> this works because Name and Synopsis are passed as array to -Property
>> parameter.
>>
>> get-help about* | Format-Table -Property Name Synopsis
>> this doesn't work, because space is delimiter between parameters.
>> Therefore PowerShell tries to bind "Synopsis" to the next positional
>> paramter and uneble to find. The same behaviour is with passing arguments
>> to functions. If you pass arguments separating them with coma - they will
>> passed as array and single argument. If you separate arguments with
>> spaces - each parameter will be passed as different argument.
My System SpecsSystem Spec
Old 07-13-2009   #6 (permalink)
Hans Dingemans


 
 

Re: commas in PowerShell

Also: try to get used to the way the help info is formatted:

Format-Table [-DisplayError] [-ShowError] [-Force] [-InputObject
<psobject>] [
-Expand <string>] [-View <string>] [-HideTableHeaders] [-AutoSize]
[-Wrap] [-G
roupBy <Object>] [[-Property] <Object[]>] [<CommonParameters>]

Note that the -Property parameter expects an array of obects, indicated by
the [] characters, that means one or more values seperated by commas. The
View parameter e.g. accepts only one string, that is the name of a view.


"Vadims Podans [MVP]" <vpodans> wrote in message
news:uohaf3lAKHA.3696@xxxxxx
Quote:

> get-help about* | Format-Table -Property Name, Synopsis
> this works because Name and Synopsis are passed as array to -Property
> parameter.
>
> get-help about* | Format-Table -Property Name Synopsis
> this doesn't work, because space is delimiter between parameters.
> Therefore PowerShell tries to bind "Synopsis" to the next positional
> paramter and uneble to find. The same behaviour is with passing arguments
> to functions. If you pass arguments separating them with coma - they will
> passed as array and single argument. If you separate arguments with
> spaces - each parameter will be passed as different argument.
> --
> WBR, Vadims Podans
> MVP: PowerShell
> PowerShell blog - www.sysadmins.lv
>
> "Larry__Weiss" <lfw@xxxxxx> rakstija zinojuma
> "news:ukPAzvlAKHA.1380@xxxxxx"...
Quote:

>> This works
>> get-help about* | Format-Table -Property Name, Synopsis
>>
>> This does not work
>> get-help about* | Format-Table -Property Name Synopsis
>>
>> What is the significance of that comma that makes the first version
>> meaningful?
>>
>> - Larry
>
My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
Installing PowerShell dependent features on W2K8 with PowerShell CTP PowerShell
Powershell Plus - Free for non commercial Use and Powershell Analyzer1.0 released PowerShell
Automatic PowerShell Error Parsing in PowerShell Analyzer and PowerShellPlus PowerShell
parsing csv files with fields that may contain commas PowerShell
PowerShell Leaders Join Forces and offer a pre-release version of PowerShell for 50% off the retail value PowerShell


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
Designer Media Ltd

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 40 41 42 43 44 45 46