New variables with the variable cmdlets

 

So you have some data in csv format:

column1 column2 column3 column4
——- ——- ——- ——-
a1      b1      c1      d1
a1      b2      c1      d1
a1      b3      c1      d1
a2      b3      c1      d1
a2      b4      c1      d1
a2      b3      c1      d1
a3      b5      c1      d1
a3      b6      c1      d1
a3      b7      c1      d1
a4      b5      c1      d1

In a variable $cd

You want colum1 and column2 in a new variable

Simplest way is probably

$new1 = $cd | select Column1, column2

The *-Variable cmdlets don’t get out much so I thought examples using them would be useful

You could also use the New-Variable cmdlet

New-Variable -Name new2 -Value ($cd | select Column1, column2)

Set-Variable also works

Set-Variable -Name new3 -Value ($cd | select Column1, column2)

Advertisements
This entry was posted in Powershell Basics. Bookmark the permalink.

2 Responses to New variables with the variable cmdlets

  1. Dominic Corbin says:

    Not to be a downer, but New-Variable and Set-Variable seem more complicated for no practical benefit. Why would I choose them over the simpler =?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s