Skip to content

Instantly share code, notes, and snippets.

@kdoblosky
kdoblosky / gist:9245780
Created February 27, 2014 07:17
Creating Objects With Methods in PowerShell - Part 3
# Code from my blog entry at http://betterlivingthroughcode.blogspot.com/2014/02/creating-objects-with-methods-in_27.html
$myObject -is [MyType]
$myObject.PSObject.TypeNames.Insert(0, "MyType")
$myObject.PSObject.TypeNames[0] -eq "MyType"
$myObject | Add-Member -MemberType ScriptMethod -Name MyMethod -Value { param([string]$p1, [int]$p2) $p1 * $p2 }
$myObject | Get-Member
@kdoblosky
kdoblosky / gist:9183113
Created February 24, 2014 06:56
Creating objects with methods in PowerShell - Part 2
# Code from my blog entry at http://betterlivingthroughcode.blogspot.com/2014/02/creating-objects-with-methods-in.html
$myObj1 = New-Object -TypeName PSObject -Property @{Prop1 = "1"; Prop2 = 2}
$myObj2 = [pscustomobject]@{Prop1 = "1"; Prop2 = 2}
$myObj2 | Get-Member
$myObj2.Prop1 = 42
$myObj2 | Get-Member
@kdoblosky
kdoblosky / gist:9167856
Created February 23, 2014 06:26
Creating objects with methods in PowerShell - Part 1
# Code from my blog entry at http://betterlivingthroughcode.blogspot.com/2014/02/creating-real-objects-in-powershell.html
$myObject = New-Object -TypeName PSObject
$myObject | Add-Member -MemberType NoteProperty -Name Name -Value "Kevin"
$myObject.Name
# Let's add a simple method
$myObject | Add-Member -MemberType ScriptMethod -Name SayHello -Value {"Hello $($this.Name)"}
$myObject.SayHello()
# Useful $profile addition
# If my current location is at a long path elsewhere, and I need to
# switch to C:\Scripts for a moment, I can do this:
#
# $ > ~
# Do whatever I need to do in E:\Scripts
# $ > pop
# And I'm now back at my original location