Using Variables in PS Flashcards

1
Q

All variables start with…

A

$

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Running $varName does what?

A

Echos the contents to the console.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How can we explicitly write to the host?

A

Write-Host $var

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

All variables in PS are…

A

objects

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How can I display the type of the variable?

A

$myVar.GetType()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How can I display the properties and methods of a variable?

A

Pipe the variable to Get-Member cmdlet
eg:

$var | Get-Member

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

So how could I find all the properties and methods from an object returned by Get-ChildItem?

A

Get-ChildItem | Get-Member

This actually returns a separate list for each member type that is returned. So if you set path to env: the above command will return different values than if it’s looking at a file system drive.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How do you explicitly type a variable

A

Wrap it in square brackets e.g:

[System.Int32]$myInt = 42

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What are the shortcuts for the .NET datatypes

A
[int]
[string]
[float]
[short]
[decimal]
[single]
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

When should we strongly type variables

A

When it is required by something that we’re working with. Most of the time we just use the dynamic typing

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

For non-string datatypes, how do we use methods on literal values?

A

Wrap it in parentheses:

e.g. (33).GetType()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What does wrapping in parentheses on a literal values actually do?

A

Forces PowerShell to convert it to an object.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

How do you specify:
>
=

A
  • gt
  • lt
  • eq
  • ne
  • le
  • ge
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

For collections, what operators are available?

A
  • in
  • notin
  • Like
  • NotLike
  • Match (matches on regex)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

How is implicit type coercion performed in PS?

A

Whatever value is on the right is converted to the data type on the left. So
42 -eq “042” == $True ..but
“042” -eq 42 == $False

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

How are true, false and null represented in PS?

A

$true
$false
$NULL

17
Q

What is the built in variable for getting the current directory?

A

$pwd

Print Working Directory

18
Q

What is the built in variable for getting the user’s home directory

A

$Home

19
Q

How can we view info about a users scripting environment?

A

$Host

20
Q

How can we view the current process id?

A

$PID

21
Q

How do we get the current version of powershell?

A

$PSVersionTable

22
Q

How is the “current item” represented in PS?

A

$_

23
Q

How do you explcitly create a new variable in PS?

A

New-Variable -Name varName -Value varValue

Note that you don’t use the $ at the beginning of the variable name

24
Q

Once a variable is created can you create it again with New-Variable?

A

No, you have to use Set-Variable instead
E.g.
Set-Variable -Name varName -Value varValue

25
Q

How do you explicitly get the value of a variable?

A

Get-Variable varName -valueonly

Again, no $ at the beginning of the variable name.

26
Q

Calling Get-Variable varName without the -valueonly switch returns?

A

A table with the variable name and the value.

27
Q

How do you get all variables?

A

Get-Variable

28
Q

How can we clear the value of a variable?

A

Clear-Variable -Name varName

29
Q

How do we totally get rid of a variable?

A

Remove-Variable -Name varName