PowerShellISEModule.psm1

Function Set-Profile
{
 psedit $profile
} #end function set-profile

Function Add-Help
{
  <#
   .Synopsis
    This function adds help at current insertion point
   .Example
    add-help
    adds comment based help at current insertion point
   .Notes
    NAME: Add-Help
    AUTHOR: ed wilson, msft
    LASTEDIT: 09/07/2010 17:32:34
    HSG: WES-09-11-10
    KEYWORDS: Scripting Techniques, Windows PowerShell ISE
   .Link
     Http://www.ScriptingGuys.com
 #Requires -Version 2.0
 #>

 $helpText = @"
 <#
   .Synopsis
    This does that
   .Description
    This function does
   .Example
    Example-
    Example- accomplishes
   .Parameter
    The parameter
   .Notes
    NAME: Example-
    AUTHOR: ed wilson, msft
    LASTEDIT: $(Get-Date)
    KEYWORDS:
    HSG:
   .Link
     Http://www.ScriptingGuys.com
 #Requires -Version 2.0
 #>
"@

 $psise.CurrentFile.Editor.InsertText($helpText)
} #end function add-help

Function Add-HeaderToScript
{
  <#
   .Synopsis
    This function adds header information to a script
   .Example
    Add-HeaderToScript
    Adds header comments to script
   .Example
    AH
    Uses alias to add header comments to script
   .Notes
    NAME: Add-HeaderToScript
    AUTHOR: ed wilson, msft
    LASTEDIT: $(Get-Date)
    KEYWORDS: $keyword
    HSG: $hsg
   .Link
     Http://www.ScriptingGuys.com
 #Requires -Version 2.0
 #>

 Param($keyword, $comment, $hsg)
 $header = @"
# -----------------------------------------------------------------------------
# Script: $(split-path -Path $psISE.CurrentFile.FullPath -Leaf)
# Author: ed wilson, msft
# Date: $(Get-Date)
# Keywords: $keyword
# comments: $comment
#
# -----------------------------------------------------------------------------
"@

 $psise.CurrentFile.Editor.InsertText($header)
} #end function add-headertoscript

Function Add-SBSBookHeaderToScript
{
  <#
   .Synopsis
    This function adds header information to a script
   .Example
    Add-SBSBookHeaderToScript -chapter 8
    Adds SBS Book header comments to script for chapter 8
   .Example
    ABH -chapter 19
    Uses alias to add SBS Book header comments to script
    Adds the chapter 19 portion
   .Example
    ABH -chapter 19 -keyword "Error Handling"
   .Notes
    NAME: Add-SBSBookHeaderToScript
    AUTHOR: ed wilson, msft
    LASTEDIT: 09/07/2010 19:37:28
    KEYWORDS: $keyword
    HSG: WES-09-12-10
   .Link
     Http://www.ScriptingGuys.com
 #Requires -Version 2.0
 #>

 Param ($chapter, $keyword, $comment) 
 $header = @"
# -----------------------------------------------------------------------------
# Script: $(split-path -Path $psISE.CurrentFile.FullPath -Leaf)
# Author: ed wilson, msft
# Date: $(Get-Date)
# Keywords: $keyword
# comments: $comment
# PowerShell 3.0 Step-by-Step, Microsoft Press, 2012
# Chapter $chapter
# -----------------------------------------------------------------------------
"@

 $psise.CurrentFile.Editor.InsertText($header)
} #end function add-SBSBookHeadertoscript

# I use Get-logNameFromDate and Start-iseTranscript together. Get-logNameFromDate
# is called from Start-iseTranscript

Function Get-logNameFromDate
{
  <#
   .Synopsis
    Creates a log name from date
   .Description
    This script creates a log from a date.
   .Example
    Get-logNameFromDate -path "c:\fso" -name "log"
    Creates a file name like c:\fso\log20100914-122019.Txt but does not
    create the file. It returns the file name to calling code.
   .Example
    Get-logNameFromDate -path "c:\fso" -name "log" -Create
    Creates a file name like c:\fso\log20100914-122019.Txt and
    create the file. It returns the file name to calling code.
   .Parameter path
    path to log file
   .Parameter name
    base name of log file
   .Parameter create
    switch that determines whether log file or only name is created
   .inputs
    [string]
   .outputs
    [string]
   .Notes
    NAME: Get-LogNameFromDate
    AUTHOR: ed wilson, msft
    LASTEDIT: 09/10/2010 16:58:06
    KEYWORDS: parameter substitution, format specifier, string substitution
    HSG: WES-09-25-10
   .Link
     Http://www.ScriptingGuys.com
 #Requires -Version 2.0
 #>

 Param(
  [string]$path = "c:\fso",
  [string]$name = "log",
  [switch]$Create
 )
 $logname = "{0}\{1}{2}.{3}" -f $path,$name, `
    (Get-Date -Format yyyyMMdd-HHmmss),"Txt"
 if($create) 
  { 
   New-Item -Path $logname -ItemType file -force | out-null
   $logname
  }
 else {$logname}
} # end function get-lognamefromdate

Function Start-iseTranscript
{
  <#
   .Synopsis
    This captures output from a script to a created text file
   .Example
    Start-iseTranscript -logname "c:\fso\log.txt"
    Copies output from script to file named xxxxlog.txt in c:\fso folder
   .Parameter logname
    the name and path of the log file.
   .inputs
    [string]
   .outputs
    [io.file]
   .Notes
    NAME: Start-iseTranscript
    AUTHOR: ed wilson, msft
    LASTEDIT: 09/10/2010 17:27:22
    KEYWORDS:
    HSG: WES-09-25-10
   .Link
     Http://www.ScriptingGuys.com
 #Requires -Version 2.0
 #>

  Param(
   [string]$logname = (Get-logNameFromDate -path $transcriptHome -name "log" -Create)
  )
  $transcriptHeader = @"
**************************************
Windows PowerShell ISE Transcript Start
Start Time: $(get-date)
UserName: $env:username
UserDomain: $env:USERDNSDOMAIN
ComputerName: $env:COMPUTERNAME
Windows version: $((Get-WmiObject win32_operatingsystem).version)
**************************************
Transcript started. Output file is $logname
"@

 $transcriptHeader >> $logname
 $psISE.CurrentPowerShellTab.Output.Text >> $logname
} #end function start-iseTranscript

Function backUp-Profile()
{
 Param([string]$destination = $backupHome)
 $dte = get-date
 $buName = $dte.tostring() -replace "[\s:{/}]","_"
 $buName = $buName + "_Profile"
 if(!(test-path $destination)) {New-Item -Path $destination -ItemType directory -force | out-null}
 copy-item -path $profile -destination "$destination\ISE$buName.ps1" -force
} #end backup-Profile

#added Functions from New-ModulesDrive.ps1
# HSG-1-20-10
Function New-ModuleDrives
{
<#
    .SYNOPSIS
    Creates two PSDrives: myMods and sysMods
    .EXAMPLE
    New-ModuleDrives
    Creates two PSDrives: myMods and sysMods. These correspond
    to the users' modules folder and the system modules folder respectively.
#>

 $driveNames = "myMods","sysMods"

 For($i = 0 ; $i -le 1 ; $i++)
 {
  New-PsDrive -name $driveNames[$i] -PSProvider filesystem `
  -Root ($env:PSModulePath.split(";")[$i]) -scope Global |
  Out-Null
 } #end For
} #end New-ModuleDrives

Function Get-FileSystemDrives
{
<#
    .SYNOPSIS
    Displays global PS Drives that use the Filesystem provider
    .EXAMPLE
    Get-FileSystemDrives
    Displays global PS Drives that use the Filesystem provider
#>

 Get-PSDrive -PSProvider FileSystem -scope Global
} #end Get-FileSystemDrives


Function Remove-AliasFromScript
{
 Get-Alias |
  Select-Object name, definition | 
  Foreach-object -begin {$a = @{} } `
                 -process { $a.add($_.name,$_.definition)} `
                 -end {}
 $b = $errors = $null
 $b = $psISE.CurrentFile.Editor.Text
 [system.management.automation.psparser]::Tokenize($b,[ref]$errors) |
 Where-Object { $_.type -eq "command" } |
 ForEach-Object {
   if($a.($_.content))
     {
      $b = $b -replace
       ('(?<=(\W|\b|^))' + [regex]::Escape($_.content) + '(?=(\W|\b|$))'),
       $a.($_.content)
     } #end if content
   } # end foreach-object
 $ScriptWithoutAliases = $psISE.CurrentPowerShellTab.Files.Add()
 $ScriptWithoutAliases.Editor.Text = $b
} #end function Remove-AliasFromScript

Function Copy-ScriptToNewTab
{
  <#
   .Synopsis
    This function copyies the contents of one tab on ISE to a new tab
   .Description
    This function copies the contents of one tab on ISE to a new tab. It also
    will copy a highlighted selection to a new tab as well.
   .Example
    Copy-ScriptToNewTab
    Copies entire contents of current script pane to a new tab in the ISE
   .Example
    Copy-ScriptToNewTab -selection
    Copies only the highlighted selection of current script pane to a new tab
    in the ISE
   .Parameter Selection
    This switched parameter causes function to only copy a selection instead of
    copying the entire contents of the script.
   .Notes
    NAME: Copy-ScriptToNewTab
    AUTHOR: ed wilson, msft
    LASTEDIT: 06/10/2012 10:00:40
    KEYWORDS: Scripting Techniques, Windows PowerShell ISE
    HSG: HSG-6-16-2012
   .Link
     Http://www.ScriptingGuys.com
 #Requires -Version 2.0
 #>

 Param([switch]$selection)
  if($selection)
   { $newtab = $psISE.CurrentPowerShellTab.Files.Add()
     $newtab.Editor.Text = $psise.CurrentFile.Editor.SelectedText }
  ELSE
   { $newtab = $psISE.CurrentPowerShellTab.Files.Add()
     $newtab.Editor.Text = $psISE.CurrentFile.Editor.text } 
} #end function Copy-ScriptToNewTab

function move-text
{
  <#
   .Synopsis
    This function will indent text in the ISE a specific number
   .Description
    This function will indent selected text in the PowerShell ISE. These are
    real spaces, not tabs. Therefore this is appropriate for situations where
    an actual tab "`t" will not work.
   .Example
    move-text -space 5
    moves selected text five spaces
   .Parameter spaces
    The number of spaces to indent the selected text. Note this number cannot
    be a negative number, and this function does not "unindent" the selected text.
   .Notes
    NAME: Move-text
    AUTHOR: ed wilson, msft
    LASTEDIT: 06/11/2012 17:16:29
    KEYWORDS: Windows PowerShell ISE, Scripting Techniques
    HSG: wes-6-17-12
   .Link
     Http://www.ScriptingGuys.com
 #Requires -Version 2.0
 #>

 Param([int]$space = 1)
 $tab = " " * $space
 $text = $psISE.CurrentFile.editor.selectedText
 foreach ($l in $text -split [environment]::newline)
  {
   $newText += "{0}{1}" -f ($tab + $l),[environment]::newline 
  }
   $psISE.CurrentFile.Editor.InsertText($newText)
} #end function move-text

## New functions added for Weekend Scripter 5/18 and 5/19 2013
Function Add-RemarkedText
{
 <#
   .Synopsis
    This function will add a remark # character to beginning of line
   .Description
    This function will add a remark character # to selected text in the ISE.
    These are comment characters, and is great when you want to comment out
    a section of PowerShell code.
   .Example
    Add-RemarkedText
    adds the comment / remark character to beginning of each selected line
   .Notes
    NAME: Add-RemarkedText
    AUTHOR: ed wilson, msft
    LASTEDIT: 05/16/2013
    KEYWORDS: Windows PowerShell ISE, Scripting Techniques
    HSG: wes-5-18-13
   .Link
     Http://www.ScriptingGuys.com
 #Requires -Version 2.0
 #>

 $text = $psISE.CurrentFile.editor.selectedText
 foreach ($l in $text -split [environment]::newline)
  {
   $newText += "{0}{1}" -f ("#" + $l),[environment]::newline 
  }
   $psISE.CurrentFile.Editor.InsertText($newText)
} #End function add-remarkedtext

Function Remove-RemarkedText
{
 <#
   .Synopsis
    This function will remove a remark # character to beginning of line
   .Description
    This function will remove a remark character # to selected text in the ISE.
    These are comment characters, and is great when you want to clean up a
    previously commentted out section of PowerShell code.
   .Example
    Remove-RemarkedText
    Removes the comment / remark character to beginning of each selected line
   .Notes
    NAME: Add-RemarkedText
    AUTHOR: ed wilson, msft
    LASTEDIT: 05/16/2013
    KEYWORDS: Windows PowerShell ISE, Scripting Techniques
    HSG: wes-5-18-13
   .Link
     Http://www.ScriptingGuys.com
 #Requires -Version 2.0
 #>

 $text = $psISE.CurrentFile.editor.selectedText
 foreach ($l in $text -split [environment]::newline)
  {
   $newText += "{0}{1}" -f ($l -replace '#',''),[environment]::newline 
  }
   $psISE.CurrentFile.Editor.InsertText($newText)
} #End function remove-remarkedtext

Function Edit-Module
{
  <#
   .Synopsis
    This opens a module stored in the $env:PSModulePath location on a new tab in ISE
   .Description
    This function uses Get-Module to retrieve a module from $env:PSModulePath and then
    it opens the module from that location into a new tab in ISE for editing. Wildcard
    characters that resolve to a single module are supported.
   .Example
    Edit-Module PowerShellISEModule
    Edit-Module PowerShellISEModule opens the PowerShellISEModule into a new tab in the
    ISE for editing
  .Example
    Edit-Module PowerShellISE*
    Edit-Module PowerShellISE* opens the PowerShellISEModule into a new tab in the
    ISE for editing by using a wild card character for the module name
   .Parameter Name
    The name of the module. Wild cards that resolve to a single module are supported
   .Notes
    NAME: Edit-Module
    AUTHOR: ed wilson, msft
    LASTEDIT: 05/16/2013 18:14:19
    KEYWORDS: Scripting Techniques, Modules
    HSG: WES-5-18-2013
   .Link
     Http://www.ScriptingGuys.com
 #Requires -Version 2.0
 #>

 Param($name)
 ISE (Get-Module -ListAvailable $name).path
} #end function Edit-Module

Function Import-EveryModule
{
  <#
   .Synopsis
    This imports all modules from the $env:PSModulePath
   .Description
    This function imports all modules from $env:psmodulepath
   .Example
    Import-EveryModule
    Import-EveryModule imports all modules from $env:psmodulepath
   .Notes
    NAME: Import-EveryModule
    AUTHOR: ed wilson, msft
    LASTEDIT: 05/16/2013 18:24:26
    KEYWORDS: Scripting Techniques, Modules
    HSG: Wes-5-18-2013
   .Link
     Http://www.ScriptingGuys.com
 #Requires -Version 2.0
 #>

 Get-Module -ListAvailable |
 Import-Module -Force
} #end function Import-Everymodule

Function Switch-OutlineView
{
  <#
   .Synopsis
    This function toggles the outline view in the ISE
   .Description
    This function toggles the outline view in the ISE. It will expand
    or collapse all functions in the current script pane.
   .Example
    Switch-OutlineView
    Switch-OutlineView will either expand or collapse all functions
   .Notes
    NAME: Switch-OutlineView
    AUTHOR: ed wilson, msft
    LASTEDIT: 05/16/2013 19:28:37
    KEYWORDS: Scripting Techniques, Modules
    HSG: wes-5-18-2013
   .Link
     Http://www.ScriptingGuys.com
 #Requires -Version 3.0
 #>

 $psise.CurrentFile.Editor.ToggleOutliningExpansion()
} #end function switch-outlineview

# *** Alias ***
if(!(Test-Path alias:ah))
 {
  New-Alias -Name ah -Value add-headertoscript -Description "MrEd alias" |
  Out-Null
  }
if(!(Test-Path alias:abh))
 {
  New-Alias -Name abh -Value Add-SBSBookHeaderToScript -Description "MrEd alias" |
  Out-Null
  }
if(!(Test-Path alias:ahlp))
 {
  New-Alias -Name ahlp -Value add-help -Description "MrEd alias" |
  Out-Null
  }
  if(!(Test-Path alias:ras))
 {
  New-Alias -Name ras -Value Remove-AliasFromScript -Description "MrEd alias" |
  Out-Null
  }
if(!(Test-Path alias:gfsd))
 {
  New-Alias -Name gfsd -Value Get-FileSystemDrives -Description "MrEd alias" |
  Out-Null
  }
if(!(Test-Path alias:cs))
 {
  New-Alias -Name cs -value Copy-ScriptToNewTab -Description "MrEd alias" |
  Out-Null
  }
if(!(Test-Path alias:ar))
 {
  New-Alias -Name ar -value Add-RemarkedText -Description "MrEd alias" |
  Out-Null
  }
if(!(Test-Path alias:rr))
 {
  New-Alias -Name rr -value Remove-RemarkedText -Description "MrEd alias" |
  Out-Null
  }
if(!(Test-Path alias:em))
 {
  New-Alias -Name em -value Edit-Module -Description "MrEd alias" |
  Out-Null
  }
if(!(Test-Path alias:iem))
 {
  New-Alias -Name iem -value Import-EveryModule -Description "MrEd alias" |
  Out-Null
  }
if(!(Test-Path alias:sov))
 {
  New-Alias -Name sov -value Switch-OutLineView -Description "MrEd alias" |
  Out-Null
  }


# *** Variables ***
if(!(Test-Path variable:moduleHome))
{
 new-variable -name moduleHome -value "$env:userProfile\documents\WindowsPowerShell\Modules"
}
if(!(Test-Path variable:backupHome))
{
 new-variable -name backupHome -value "$env:userProfile\documents\WindowsPowerShell\profileBackup"
}
if(!(Test-Path variable:TranscriptHome))
{
 new-variable -name TranscriptHome -value "$env:userProfile\documents\WindowsPowerShell\transcript"
}

Export-ModuleMember -alias * -function * -variable *