This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |