PowerShell 101+

Beyond the Basics

Top 10 CmdLets

Types / Typing

MagDev Favorites

Top 10

The top ten commands every admin should know

#1

Get-Help

#2

Set-ExecutionPolicy

Get-

#3

Pipe   " | "

Takes output from one command and sends it to the next

This is a form of ITERATION

#4

Get-Service

Start-

Stop-

Restart-

#5

Get-Process

Stop-

#6

Export-CSV

#7

Get-EventLog

#8

Where-Object

Select-

#9

For()

ForEach(x in y)

ForEach-Object

#10

Switch

Types

Really it does help!

Simple Syntax

[type]$variable

Context-aware dot (.) intelligence

.Net Data Types

 

Data Types

[string]    Fixed-length string of Unicode characters
[char]      A Unicode 16-bit character
[byte]      An 8-bit unsigned character

[int]       32-bit signed integer
[long]      64-bit signed integer
[bool]      Boolean True/False value

[decimal]   A 128-bit decimal value
[single]    Single-precision 32-bit floating point number
[double]    Double-precision 64-bit floating point number

[DateTime]  Date and Time

[xml]       Xml object
[array]     An array of values
[hashtable] Hashtable object

WAT

Automatic Type Conversion

Automatic conversion does not change the variable’s data type.

MagDev Favs!

The fun parts of PowerShell!

 

Well in my insanity it is....

String Format

$a = "is"
$b = "amazing"
$str = "This {0} an {1} feature!" -f $a, $b

 

WAY better than:

$str = "This" + $a + "an" + $b + "feature!"  
--or--
$str = "This $($a) an $($b) feature!"

Switch

But now how you think....

switch ($true){
    (1 -eq 1) {
        write-host "int"        
    }
    ("car" -eq "car") {
        write-host "string"        
    }
    default {
        write-host "hahaha"
    }

}

Alternate: Only one Truthy
(1 -eq 1) {
        write-host "int" 
        break       
}

Json

Stop using CSV for a data-store


ConvertTo-Json

ConvertFrom-Json

 

Classes

This could be an entire session

 

But seriously - they are amazing!

 

PowerShell 101+

By Ken Maglio

PowerShell 101+

Beyond the basics

  • 613