![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| 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) |
| | $Variables I have been trying to use a $variable to control my format-table command. --------------------------------------------------------------------------------------------------------- $Item = "DeviceId, MediaType, Size, FreeSpace" get-wmiobject -query "Select $Item from win32_logicaldisk" ` |sort mediatype | format-table $Item -auto -groupby MediaType # |sort mediatype | format-table $ExecutionContext.InvokeCommand.ExpandString($Item) -auto -groupby MediaType -------------------------------------------------------------------------------------------------------------- As you can see from the remmed out line I have been trying a variety of techniques. While I have learnt a lot about , $ExecutionContext.InvokeCommand.ExpandString and defining variables with [string] and other [types], I have not solved my problem. Here is a version that does work perfectly well, but I love employing variables. -------------------------------------------------------------------------------------------------------------- get-wmiobject -query "select DeviceId, Status, MediaType, Size, FreeSpace from win32_logicaldisk"` |sort | format-table DeviceId, MediaType, Size, FreeSpace -groupby MediaType -auto Can you help get it working with a $Item variable? Guy |
My System Specs![]() |
| | #2 (permalink) |
| | Re: $Variables On May 31, 9:02 am, "Guy Thomas" <g...@computerperformance.co.uk> wrote: > I have been trying to use a $variable to control my format-table command. > > ---------------------------------------------------------------------------*------------------------------ > > $Item = "DeviceId, MediaType, Size, FreeSpace" > > get-wmiobject -query "Select $Item from win32_logicaldisk" ` > > |sort mediatype | format-table $Item -auto -groupby MediaType > > # |sort mediatype | format-table > $ExecutionContext.InvokeCommand.ExpandString($Item) -auto -groupby MediaType > > ---------------------------------------------------------------------------*----------------------------------- > > As you can see from the remmed out line I have been trying a variety of > techniques. While I have learnt a lot about , > $ExecutionContext.InvokeCommand.ExpandString and defining variables with > [string] and other [types], I have not solved my problem. > > Here is a version that does work perfectly well, but I love employing > variables. > > ---------------------------------------------------------------------------*----------------------------------- > > get-wmiobject -query "select DeviceId, Status, MediaType, Size, FreeSpace > from win32_logicaldisk"` > > |sort | format-table DeviceId, MediaType, Size, FreeSpace -groupby > MediaType -auto > > Can you help get it working with a $Item variable? > > Guy You should make your variable an array, because that is what is passed to format-table when you use the literal syntax, e.g.: $item = @("DeviceId", "MediaType", "Size", "FreeSpace") get-wmiobject -query "Select $Item from win32_logicaldisk" ` | sort mediatype | format-table $Item -auto -groupby MediaType Hope this helps, - Oisin |
My System Specs![]() |
| | #3 (permalink) |
| | Re: $Variables "Oisin Grehan" <oising@gmail.com> wrote in message news:1180619962.555154.39380@q69g2000hsb.googlegroups.com... On May 31, 9:02 am, "Guy Thomas" <g...@computerperformance.co.uk> wrote: > I have been trying to use a $variable to control my format-table command. > > ---------------------------------------------------------------------------*------------------------------ > > $Item = "DeviceId, MediaType, Size, FreeSpace" > > get-wmiobject -query "Select $Item from win32_logicaldisk" ` > > |sort mediatype | format-table $Item -auto -groupby MediaType > > # |sort mediatype | format-table > $ExecutionContext.InvokeCommand.ExpandString($Item) -auto -groupby > MediaType > > ---------------------------------------------------------------------------*----------------------------------- > > As you can see from the remmed out line I have been trying a variety of > techniques. While I have learnt a lot about , > $ExecutionContext.InvokeCommand.ExpandString and defining variables with > [string] and other [types], I have not solved my problem. > > Here is a version that does work perfectly well, but I love employing > variables. > > ---------------------------------------------------------------------------*----------------------------------- > > get-wmiobject -query "select DeviceId, Status, MediaType, Size, FreeSpace > from win32_logicaldisk"` > > |sort | format-table DeviceId, MediaType, Size, FreeSpace -groupby > MediaType -auto > > Can you help get it working with a $Item variable? > > Guy You should make your variable an array, because that is what is passed to format-table when you use the literal syntax, e.g.: $item = @("DeviceId", "MediaType", "Size", "FreeSpace") get-wmiobject -query "Select $Item from win32_logicaldisk" ` | sort mediatype | format-table $Item -auto -groupby MediaType Hope this helps, - Oisin Dear Oisin I thank you. The array worked as you said it would. The only problem is that I (we) cannot make one variable work for both the -query and the format-table. - or can we? ------------------------------------------------------------------------------------------ $Seln = "DeviceId, MediaType, Size, FreeSpace" $Item = @("DeviceId", "MediaType", "Size", "FreeSpace") get-wmiobject -query "Select $Seln from win32_logicaldisk"` |sort mediatype | format-table $Item -auto -groupby MediaType ------------------------------------------------------------------------------------------ Is there anyway of using just one variable, instead of both $Seln and $Item? Guy |
My System Specs![]() |
| | #4 (permalink) |
| | Re: $Variables > ------------------------------------------------------------------------------------------ > > $Seln = "DeviceId, MediaType, Size, FreeSpace" > > $Item = @("DeviceId", "MediaType", "Size", "FreeSpace") > > get-wmiobject -query "Select $Seln from win32_logicaldisk"` > > |sort mediatype | format-table $Item -auto -groupby MediaType > > ------------------------------------------------------------------------------------------ > > Is there anyway of using just one variable, instead of both $Seln and > $Item? Well.... you could always try creating $Seln from $Item: PS C:\> $Item = @("DeviceId", "MediaType", "Size", "FreeSpace") PS C:\> $seln = [string]::Join(",", $Item) PS C:\> $seln DeviceId,MediaType,Size,FreeSpace "Guy Thomas" <guy@computerperformance.co.uk> wrote in message news:u5iFOQ6oHHA.3952@TK2MSFTNGP03.phx.gbl... > > "Oisin Grehan" <oising@gmail.com> wrote in message > news:1180619962.555154.39380@q69g2000hsb.googlegroups.com... > On May 31, 9:02 am, "Guy Thomas" <g...@computerperformance.co.uk> > wrote: >> I have been trying to use a $variable to control my format-table command. >> >> ---------------------------------------------------------------------------*------------------------------ >> >> $Item = "DeviceId, MediaType, Size, FreeSpace" >> >> get-wmiobject -query "Select $Item from win32_logicaldisk" ` >> >> |sort mediatype | format-table $Item -auto -groupby MediaType >> >> # |sort mediatype | format-table >> $ExecutionContext.InvokeCommand.ExpandString($Item) -auto -groupby >> MediaType >> >> ---------------------------------------------------------------------------*----------------------------------- >> >> As you can see from the remmed out line I have been trying a variety of >> techniques. While I have learnt a lot about , >> $ExecutionContext.InvokeCommand.ExpandString and defining variables with >> [string] and other [types], I have not solved my problem. >> >> Here is a version that does work perfectly well, but I love employing >> variables. >> >> ---------------------------------------------------------------------------*----------------------------------- >> >> get-wmiobject -query "select DeviceId, Status, MediaType, Size, >> FreeSpace >> from win32_logicaldisk"` >> >> |sort | format-table DeviceId, MediaType, Size, FreeSpace -groupby >> MediaType -auto >> >> Can you help get it working with a $Item variable? >> >> Guy > > You should make your variable an array, because that is what is passed > to format-table when you use the literal syntax, e.g.: > > $item = @("DeviceId", "MediaType", "Size", "FreeSpace") > > get-wmiobject -query "Select $Item from win32_logicaldisk" ` > | sort mediatype | format-table $Item -auto -groupby MediaType > > Hope this helps, > > - Oisin > > Dear Oisin > > I thank you. The array worked as you said it would. The only problem is > that I (we) cannot make one variable work for both the -query and the > format-table. - or can we? > > ------------------------------------------------------------------------------------------ > > $Seln = "DeviceId, MediaType, Size, FreeSpace" > > $Item = @("DeviceId", "MediaType", "Size", "FreeSpace") > > get-wmiobject -query "Select $Seln from win32_logicaldisk"` > > |sort mediatype | format-table $Item -auto -groupby MediaType > > ------------------------------------------------------------------------------------------ > > Is there anyway of using just one variable, instead of both $Seln and > $Item? > > Guy > > |
My System Specs![]() |
| | #5 (permalink) |
| | Re: $Variables Here is what I got to work $Item = @("DeviceId", "MediaType", "Size", "FreeSpace") # this is one long line if it should wrap get-wmiobject -query "Select $([string]::Join(',',$Item)) from win32_logicaldisk" |Format-Table $item -auto -groupby MediaType "Guy Thomas" <guy@computerperformance.co.uk> wrote in message news:u5iFOQ6oHHA.3952@TK2MSFTNGP03.phx.gbl... > > "Oisin Grehan" <oising@gmail.com> wrote in message > news:1180619962.555154.39380@q69g2000hsb.googlegroups.com... > On May 31, 9:02 am, "Guy Thomas" <g...@computerperformance.co.uk> > wrote: >> I have been trying to use a $variable to control my format-table command. >> >> ---------------------------------------------------------------------------*------------------------------ >> >> $Item = "DeviceId, MediaType, Size, FreeSpace" >> >> get-wmiobject -query "Select $Item from win32_logicaldisk" ` >> >> |sort mediatype | format-table $Item -auto -groupby MediaType >> >> # |sort mediatype | format-table >> $ExecutionContext.InvokeCommand.ExpandString($Item) -auto -groupby >> MediaType >> >> ---------------------------------------------------------------------------*----------------------------------- >> >> As you can see from the remmed out line I have been trying a variety of >> techniques. While I have learnt a lot about , >> $ExecutionContext.InvokeCommand.ExpandString and defining variables with >> [string] and other [types], I have not solved my problem. >> >> Here is a version that does work perfectly well, but I love employing >> variables. >> >> ---------------------------------------------------------------------------*----------------------------------- >> >> get-wmiobject -query "select DeviceId, Status, MediaType, Size, >> FreeSpace >> from win32_logicaldisk"` >> >> |sort | format-table DeviceId, MediaType, Size, FreeSpace -groupby >> MediaType -auto >> >> Can you help get it working with a $Item variable? >> >> Guy > > You should make your variable an array, because that is what is passed > to format-table when you use the literal syntax, e.g.: > > $item = @("DeviceId", "MediaType", "Size", "FreeSpace") > > get-wmiobject -query "Select $Item from win32_logicaldisk" ` > | sort mediatype | format-table $Item -auto -groupby MediaType > > Hope this helps, > > - Oisin > > Dear Oisin > > I thank you. The array worked as you said it would. The only problem is > that I (we) cannot make one variable work for both the -query and the > format-table. - or can we? > > ------------------------------------------------------------------------------------------ > > $Seln = "DeviceId, MediaType, Size, FreeSpace" > > $Item = @("DeviceId", "MediaType", "Size", "FreeSpace") > > get-wmiobject -query "Select $Seln from win32_logicaldisk"` > > |sort mediatype | format-table $Item -auto -groupby MediaType > > ------------------------------------------------------------------------------------------ > > Is there anyway of using just one variable, instead of both $Seln and > $Item? > > Guy > > |
My System Specs![]() |
| | #6 (permalink) |
| | Re: $Variables Dear Oisin, Marcel and Brandon Your responses were fantastic - needless to say they worked a treat. I am still at that 'little boy thrilled with new toy' stage of scripting with PowerShell. I would also like to take this opportunity to say what a friendly, focused forum this is. Guy "Guy Thomas" <guy@computerperformance.co.uk> wrote in message news:u5iFOQ6oHHA.3952@TK2MSFTNGP03.phx.gbl... > > "Oisin Grehan" <oising@gmail.com> wrote in message > news:1180619962.555154.39380@q69g2000hsb.googlegroups.com... > On May 31, 9:02 am, "Guy Thomas" <g...@computerperformance.co.uk> > wrote: >> I have been trying to use a $variable to control my format-table command. >> >> ---------------------------------------------------------------------------*------------------------------ >> >> $Item = "DeviceId, MediaType, Size, FreeSpace" >> >> get-wmiobject -query "Select $Item from win32_logicaldisk" ` >> >> |sort mediatype | format-table $Item -auto -groupby MediaType >> >> # |sort mediatype | format-table >> $ExecutionContext.InvokeCommand.ExpandString($Item) -auto -groupby >> MediaType >> >> ---------------------------------------------------------------------------*----------------------------------- >> >> As you can see from the remmed out line I have been trying a variety of >> techniques. While I have learnt a lot about , >> $ExecutionContext.InvokeCommand.ExpandString and defining variables with >> [string] and other [types], I have not solved my problem. >> >> Here is a version that does work perfectly well, but I love employing >> variables. >> >> ---------------------------------------------------------------------------*----------------------------------- >> >> get-wmiobject -query "select DeviceId, Status, MediaType, Size, >> FreeSpace >> from win32_logicaldisk"` >> >> |sort | format-table DeviceId, MediaType, Size, FreeSpace -groupby >> MediaType -auto >> >> Can you help get it working with a $Item variable? >> >> Guy > > You should make your variable an array, because that is what is passed > to format-table when you use the literal syntax, e.g.: > > $item = @("DeviceId", "MediaType", "Size", "FreeSpace") > > get-wmiobject -query "Select $Item from win32_logicaldisk" ` > | sort mediatype | format-table $Item -auto -groupby MediaType > > Hope this helps, > > - Oisin > > Dear Oisin > > I thank you. The array worked as you said it would. The only problem is > that I (we) cannot make one variable work for both the -query and the > format-table. - or can we? > > ------------------------------------------------------------------------------------------ > > $Seln = "DeviceId, MediaType, Size, FreeSpace" > > $Item = @("DeviceId", "MediaType", "Size", "FreeSpace") > > get-wmiobject -query "Select $Seln from win32_logicaldisk"` > > |sort mediatype | format-table $Item -auto -groupby MediaType > > ------------------------------------------------------------------------------------------ > > Is there anyway of using just one variable, instead of both $Seln and > $Item? > > Guy > > |
My System Specs![]() |
![]() |
| Thread Tools | |
| |
Similar Threads | ||||
| Thread | Forum | |||
| SQL query with PS variables | PowerShell | |||
| math with "GB/MB/KB" in variables fails, without variables works? | PowerShell | |||
| not using variables | PowerShell | |||