# encoding: ascii
# api: powershell
# title: Invoke-AddTypeTypeDefini
# description: From Windows PowerShell Cookbook (O’Reilly) by Lee Holmes
# version: 0.1
# type: class
# author: Lee Holmes
# license: CC0
# x-poshcode-id: 2173
# x-archived: 2016-03-19T00:35:42
# x-published: 2011-09-09T21:41:00
#
#
#############################################################################
##
## Invoke-AddTypeTypeDefinition
##
## From Windows PowerShell Cookbook (O'Reilly)
## by Lee Holmes (http://www.leeholmes.com/guide)
##
##############################################################################
<#
.SYNOPSIS
Demonstrates the use of the -TypeDefinition parameter of the Add-Type
cmdlet.
#>
Set-StrictMode -Version Latest
## Define the new C# class
$newType = @'
using System;
namespace PowerShellCookbook
{
public class AddTypeTypeDefinitionDemo
{
public string SayHello(string name)
{
string result = String.Format("Hello {0}", name);
return result;
}
}
}
'@
## Add it to the Powershell session
Add-Type -TypeDefinition $newType
## Show that we can access it like any other .NET type
$greeter = New-Object PowerShellCookbook.AddTypeTypeDefinitionDemo
$greeter.SayHello("World");