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 - $Variables

Reply
 
Old 05-31-2007   #1 (permalink)
Guy Thomas


 
 

$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 SpecsSystem Spec
Old 05-31-2007   #2 (permalink)
Oisin Grehan


 
 

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 SpecsSystem Spec
Old 05-31-2007   #3 (permalink)
Guy Thomas


 
 

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 SpecsSystem Spec
Old 05-31-2007   #4 (permalink)
Marcel J. Ortiz [MSFT]


 
 

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 SpecsSystem Spec
Old 05-31-2007   #5 (permalink)
Brandon Shell


 
 

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 SpecsSystem Spec
Old 06-01-2007   #6 (permalink)
Guy Thomas


 
 

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 SpecsSystem Spec
Reply

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


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