Windows Vista Forums
Vista Forums Home Join Vista Forums Donate Vista Tutorials Tags

Welcome to Vista Forums we are your forum to discuss Windows Vista x64 and x86 systems. Whether you need help or just want to post an idea you have on Vista, this is the forum for you.
Register at Vista forums...the world biggest Windows Vista resource Join Vista Forums Now

Go Back   Vista Forums > Microsoft Technical Newsgroups > PowerShell

$Variables

Closed Thread
 
Thread Tools Display Modes
Old 05-31-2007   #1 (permalink)
Guy Thomas
Guest


 

$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


Old 05-31-2007   #2 (permalink)
Oisin Grehan
Guest


 

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

Old 05-31-2007   #3 (permalink)
Guy Thomas
Guest


 

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


Old 05-31-2007   #4 (permalink)
Marcel J. Ortiz [MSFT]
Guest


 

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


Old 05-31-2007   #5 (permalink)
Brandon Shell
Guest


 

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


Old 06-01-2007   #6 (permalink)
Guy Thomas
Guest


 

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



Closed Thread

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
not using variables IT Staff PowerShell 1 01-09-2007 09:50 PM
Re: list env variables Keith Hill PowerShell 4 01-04-2007 11:23 AM
Should this work with variables Marco Shaw PowerShell 2 12-04-2006 10:29 AM
Sundeep Raina(Iopsis): Is variables being handled in Workflow and How to declare New Variables sunny Avalon 0 06-30-2006 01:31 AM








Vistax64.com 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 2005-2008

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 47 48 49 50