10. January 2009
Andy Schneider
Thought I would share my ISE profile and test out
Write-CommandBlogPost in Live Writer with
Doug Finke’s Live Writer Add-In
Microsoft.PowerShellISE_profile.ps1
Synopsis:
Creating and using a Windows PowerShell profile
Here's Microsoft.PowerShellISE_profile.ps1:
function Set-File {
param
(
[parameter(Mandatory=$true,ValueFromPipeline=$true)]
[string]
$file
)
$psise.CurrentOpenedRunspace.OpenedFiles.Add($file)
}
function Insert-Text{
param
(
[parameter(Mandatory=$true, ValueFromPipeline=$true)]
[string]
$text
)
$currentFilePath = $psise.CurrentOpenedFile.FullPath
$currentFile = $psIse.CurrentOpenedRunspace.OpenedFiles |
where {$_.FullPath -eq $currentFilePath}
$currentFile.editor.InsertText($text)
}
function New-FunctionTemplate{
param
(
[parameter(Mandatory=$true, ValueFromPipeline=$true)]
[string]
$verb,
[parameter(Mandatory=$true, ValueFromPipeline=$true)]
[string]
$noun
)
$f = @"
function $Verb-$Noun {
param (
[parameter(Mandatory=$true, ValueFromPipeline=$true)
[string]
$p1
)
begin {}
process {}
end {}
}
"@
Insert-Text -text $f
}
function invoke-region{
param
(
[int] $num
)
$ed = $psISE.CurrentOpenedFile.Editor
$lines = [Regex]::Split($ed.text,"`r`n" )
$foundfirst = -1
$foundlast = -1
for($count = 0;$count -le $lines.length-1;$count++)
{
if ($lines[$count].startswith("#region") -and $lines[$count].contains("@$num"))
{ $foundfirst = $count;break}
}
if($foundfirst -gt -1)
{
for ($count = $foundfirst; $count -le $lines.length-1;$count++)
{
if ($lines[$count].startswith("#endregion") )
{$foundlast = $count;break}
} # end For $lines
if ($foundlast -gt -1)
{
$torun = ""
$lines[$foundfirst..$foundlast] | % { $torun+=$_ + "`r`n"}
invoke-expression $torun
} # end if $foundLast
} # End for $count
} # End Function Invoke-Region
$psise.options.CommandPaneUp = $true
$psise.options.fontname = "Consolas"
$psise.options.fontsize = 16
[void]$psIse.CustomMenu.Submenus.Add("_Function Template", {New-FunctionTemplate},"Alt+F")
[void]$psISE.CustomMenu.Submenus.Add("Region 1", {invoke-region 1 },'ctrl+1')
[void]$psISE.CustomMenu.Submenus.Add("Region 2", {invoke-region 2 },'ctrl+2')
[void]$psISE.CustomMenu.Submenus.Add("Region 3", {invoke-region 3 },'ctrl+3')
[void]$psISE.CustomMenu.Submenus.Add("Region 4", {invoke-region 4 },'ctrl+4')
[void]$psISE.CustomMenu.Submenus.Add("Region 5", {invoke-region 5 },'ctrl+5')
[void]$psISE.CustomMenu.Submenus.Add("Current Line", {invoke-caretline},'f7')
$docs = $(resolve-path "$Env:userprofiledocuments")
$desktop = $(resolve-path "$Env:userprofiledesktop")
$myMod = $(resolve-path $($Env:PSMODULEPATH -split ";")[0])
Automatically generated with a custom version of Write-CommandBlogPost