Skip to content

Instantly share code, notes, and snippets.

@tylerd
Last active August 29, 2015 13:56
Show Gist options
  • Save tylerd/9056461 to your computer and use it in GitHub Desktop.
Save tylerd/9056461 to your computer and use it in GitHub Desktop.
PowerShell Session for Winnipeg .NET User Group
#Print Strings
"Hello World"
#Run Commands
ping google.com
#Use Variables
$name = "Tyler"; $name
#Quotes and strings
"Hello $name"
# Hello Tyler
'Hello $name'
# Hello $name
#Double quotes allow for placement of variables
$a = Get-Item .
$a.Name
# outputs the current folder to the screen
"This folder is called $a.Name"
# This folder is called C:\Users\Tyler.Name
#Not exactly what I was looking for
"This folder is called $($a.Name)"
# This folder is called Tyler
#That is more like it
Get-Help Get-Item
Get-Help *-Item #returns all commands ending in "-item"
Get-Command -Module Azure -Verb Get #Get-Command is a great way to find what you can do in a module
Get-Verb #outputs the suggested verbs used in commands with context
# Arrays in PowerShell
$a = 1,2,3
$a
$a + 4
$a + 'text'
#Unless created with a known type the array does not care what the contents are
$b = 4,5,6
$a + $b
$a[0]
$a[-1] #last item
$a[0..2] #items between index 0 and 2, inclusive
1..10
@{FirstName = 'Tyler'; LastName = 'Doerksen'}
$h = @{}
$h.Name = 'Tyler'
$h2 = @{City = 'Winnipeg'}
$h + $h2
#create a file called scriptdemo1.ps1 with the following contents
# $name = 'Tyler'
# $name
#Open PowerShell ISE
$name = "not Tyler"
.\scriptdemo1.ps1
$name
# will output "not Tyler"
. .\scriptdemo1.ps1
$name
# will output "Tyler"
# The additional . in front of the script path indicates you want to run the script in the same session as the current window
# This is called dot-sourcing
function Get-Greeting([string]$Name)
{
"Hello $Name"
}
Get-Help Get-Greeting
function Get-Greeting()
{
param([string] $Name)
"Hello $Name"
}
function Get-Greeting()
{
[CmdletBinding]
param([string] $Name)
if(!$Name)
{
Write-Verbose "Unknown Name"
$Name = "Unknown"
}
"Hello $Name"
}
filter Get-Greeting()
{
[Cmdletbinding()]
param(
[Parameter(ValueFromPipeline = $true)]
[string]$Name)
if(!$Name)
{
write-verbose "Unknown Name"
$Name = "Unknown"
}
"Hello $Name"
}
"tyler", "mitch" | Get-Greeting
function Get-Greeting()
{
[Cmdletbinding()]
param(
[Parameter(ValueFromPipeline = $true)]
[string$Name)
Begin {
Write-Host "Start Greeting"
Write-Host $Name
}
Process {
if(!$Name)
{
write-verbose "Unknown Name"
$Name = "Unknown"
}
"Hello $Name"
}
End {
Write-Host "End Greeting"
}
}
"tyler", "mitch" | Get-Greeting
$g = [System.Guid];:NewGuid()
[System.AppDomain]::CurrentDomain.GetAssemblies() |
where { $_.Location } |
foreach { Split-Path -Leaf $_.Location } |
sort
$client = New-Object New.WebClient
$client | gm
# outputs the methods and properties of the class
$client | gm DownloadString | fl
Add-Type -TypeDefinition @"
public class MyMathClass {
public int Add(int n1, int n2) {
return n1 + n2;
}
}
"@
$demo = New-Object MyMathClass
$demo.Add(1,2)
[xml]$feed = $client.DownloadString("http://www/dougfinke.com/blog/index.php/feed")
$feed.rss.channel.item | select Title,pubDate
# adding members
$o = "Hello World" | Add-Member -PassThru ScriptProperty Reverse { $this[$this.Length..0] -join "" }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment