Powershell‎ > ‎Training Notes‎ > ‎

Day 3

Get-PSProvider

 

Get-PSDrive

Get-PSDrive -PSProvider registry
Get-PSDrive -PSProvider environment
Get-Command *-psdrive

Get-Help New-PSDrive -full | more

 

New-PSDrive

New-PSDrive -Name system32 -PSProvider filesystem -root ($env:windir + "\system32") -Description "Custom Drive"
cd system32:
$pwd.Path
dir function:c: | fl *
Remove-PSDrive system32

Set-Location cert:
cd cert:\CurrentUser\AuthRoot
gci | where {$_.subject -like "*c&w*"}
gci | where {$_.subject -like "*c&w*"} | Select-Object subject, thumbprint

 

Set-Location env:
dir
gci
dir | sort name
Get-Item windir
Get-Item windir | Format-List *
(Get-Item windir).value #same as line below
$env:windir #same as line above

Create a new envirnment variable

New-Item -path . -name admin -value mred
Rename-Item -path env:\admin -newname super
Remove-Item super

 

Update environment variable

Set-Location env:
Set-Item Path ($env:path + ";c:\temp")
Set-Item env:\Path ($env:path + ";c:\temp") #same as line below
$env:path = $env:path + ";c:\temp1" #same as line above
dir env:\path
get-item env:\path

Arrays

$iary = 1..25
$iary 
1

2

3

$iary[11]

12

$iary[11] = "The Middle"
$iary[11]

 The Middle

Get the number you would use next to add a new item to the array

$iary[$iary.GetUpperBound(0)]

$iary[$iary.count -1]

$iary[-1]


25

$iary[1,2,3,15]
$iary[1,2,3,15,-1,-2]
 
$iary[1,2,3,15,-1,-2, 10..14]   # Fails
$iary[1,2,3,15,-1,-2 + 10..14]
add an item to the array
$iary += "The End"

$iary[0] = $null

$ary =@()
$ary.gettype()
 add stuff to this array
$ary += "first"
$ary += "second"
$ary += "third"
 
$ary = New-Object "system.object[]" 5
$ary.GetUpperBound(0)
$ary.Count
$ary

Shift Array

Shift items in an array
 $a = 1,2,3,4,5
 $first, $rest = $a
 

multidimensional array

$2d = New-Object "system.object[,]" 5,10
$2d.rank
$2d[0,1] = "First value second dimension"
$2d
$2d[0,1]

variables

$newimplicitvarible = "blah"
New-Variable snewExplicitvariable
set-variable snewExplicitvariable -value "mred"
$var

Force declaration of variables

set-psdebug -strict:$True
$var #will now fai
Turn this off now
set-psdebug -strict:$False

Strongly type a variable

[int]$a = 1
$a.GetType()
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True Int32 System.ValueType

[string]$a = "Hello World"
$a.GetType()

Constant

Set-variable -name intDriveType -value 3 -option constant #constant for local disk

Read-only

New-Variable

Scope

Global

local

scriptlocal

numericlocal in nested

$Global:variablename = "blah"


function global:test
{
}

Execution Policy

  1. restricted (default)
  2. AllSigned
  3. RemoteSigned
  4. Unrestricted
get-command -noun ExecutionPolicy
Get-ExecutionPolicy
Set-ExecutionPolicy Unrestricted
Get-Command *authenticode*
notepad c:\filename.ps1:zone.identifier

Pass Parameters to a script

testparameters.ps1

PARAM ($server=".",$ns="root\cimv2")

Write-Host "server: " $server
Write-Host "Name Space: " $ns
.\testparameters.ps1 foo
server: foo
Name Space: root\cimv2

 

Get-Host

(get-host).currentculture.DateTimeFormat | format-list -property *
(get-host).ui.rawui | format-list -property *
(get-host).ui.rawui.backgroundcolor = "Black"
  
Comments