![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| 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. |
| |||||||
![]() |
| |
| | #1 (permalink) |
| | 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 Specs![]() |
| | #2 (permalink) |
| | 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 Specs![]() |
| | #3 (permalink) |
| | 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 Specs![]() |
| | #4 (permalink) |
| | 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 Specs![]() |
| | #5 (permalink) |
| | 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 Specs![]() |
| | #6 (permalink) |
| | 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 Specs![]() |
![]() |
| Thread Tools | |
| |