PSISE.psm1
#region New-PSISETab Function New-PSISETab { <# .SYNOPSIS Start a new powershell tab. .DESCRIPTION Start a new powershell tab within ISE. .PARAMETER Name The name of the new tab. .EXAMPLE New-PSISETab -Name MyTab This command will open a new powershell tab named MyTab. #> Param ( [string[]]$Name, [switch]$Verbose, [switch]$NoSwitch ) # Save the current tab in case we need to switch back to it $currentTab = $psISE.CurrentPowerShellTab if( $Name ) { foreach($N in $Name) { if($Verbose) { $psISE.PowerShellTabs.Add() } else { $psISE.PowerShellTabs.Add() | Out-Null } $PSISE.PowerShellTabs[-1].DisplayName = $N } } else { $psISE.PowerShellTabs.Add() | Out-Null } # Check if the user wants to remain on the new tab or switch back to the previous one if($NoSwitch) { $psISE.PowerShellTabs.SetSelectedPowerShellTab($currentTab) } } #endregion #region Rename-PSISETab Function Rename-PSISETab { <# .SYNOPSIS Rename a new powershell tab. .DESCRIPTION Rename a new powershell tab within ISE. .PARAMETER Name The name of the tab. .EXAMPLE Rename-PSISETab -Name MyTab -NewName MyTab2 This command will rename the MyTab tab to MyTab2. .EXAMPLE Rename-PSISETab -Current -NewName MyTab This command will rename the current tab to MyTab2. .EXAMPLE Rename-PSISETab -TabNumber 1 -NewName MyTab This command will rename the tab number 1 to MyTab2. #> Param ( [Parameter(ParameterSetName="TabName", Mandatory=$True, Position=0)] [string]$Name, [Parameter(ParameterSetName="TabNumber", Mandatory=$True, Position=0)] [int]$Number, [Parameter(ParameterSetName="TabCurrent", Mandatory=$True, Position=0)] [switch]$Current, [Parameter(ParameterSetName="TabName", Mandatory=$True, Position=1)] [Parameter(ParameterSetName="TabNumber", Mandatory=$True, Position=1)] [Parameter(ParameterSetName="TabCurrent", Mandatory=$True, Position=1)] [string]$NewName ) if( $Current ) { $psise.CurrentPowershellTab.DisplayName = $NewName } if( $Number ) { if( $Number -ge $psise.PowerShellTabs.Count ) { Write-Error "No such tab!" } else { $psise.PowershellTabs[$number].DisplayName = $NewName } } if( $Name ) { # Search for the tab for( $i = 0; $i -lt $psise.PowerShellTabs.Count; $i++) { if( $psise.PowerShellTabs[$i].DisplayName -eq $Name ) { $psise.PowerShellTabs[$i].DisplayName = $NewName break } } } } #endregion #region Open-PSISEFile Function Open-PSISEFile { <# .SYNOPSIS Open a file using Powershell ISE. .DESCRIPTION Open a file using Powershell ISE. .PARAMETER TabName The name of the tab in which to open the file. .PARAMETER Path The path of the file to open. .EXAMPLE Open-PSISEFile -Path TestScript.ps1 This command will open the TestScript.ps1 file on the current tab. .EXAMPLE Open-PSISEFile -Path TestScript.ps1 -TabName Exchange This command will open the TestScript.ps1 file on the tab named "Exchange". .EXAMPLE Open-PSISEFile -Path TestScript.ps1 -AsCopy This command will create a new empty file and copy the contents of TestScript.ps1 to it. #> [CmdletBinding(DefaultParametersetName='FileOnly')] Param ( [Parameter(ParameterSetName="TabName", Mandatory=$True, Position=0, ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true)] [Parameter(ParameterSetName="TabNumber", Mandatory=$True, Position=0, ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true)] [Parameter(ParameterSetName="FileOnly", Mandatory=$True, Position=0, ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true)] [string[]]$Path, [Parameter(ParameterSetName="TabNumber", Mandatory=$false, Position=1)] [int]$TabNumber, [Parameter(ParameterSetName="TabName", Mandatory=$false, Position=1)] [string]$TabName, [Parameter(ParameterSetName="TabName", Mandatory=$false)] [Parameter(ParameterSetName="TabNumber", Mandatory=$false)] [Parameter(ParameterSetName="FileOnly", Mandatory=$false)] [switch]$AsCopy ) Begin{} Process { foreach($f in $Path) { # Test if file exists if( -Not (Test-Path -Path $f) ) { Write-Error ("Could not find file " + $f) continue } # Get the full path to the file $p = Resolve-Path -Path $f # Add the file using tab name if( $TabName ) { $found = 0 for($i=0; $i -lt $psISE.PowerShellTabs.Count; $i++) { if($psISE.PowerShellTabs[$i].displayname -eq $TabName) { if($AsCopy) { $content = Get-Content $p -Raw $newFile = $psISE.PowerShellTabs[$i].Files.Add() $newFile.Editor.Text = $content $psISE.PowerShellTabs[$i].ExpandedScript = $true $newFile.Editor.SetCaretPosition(1,1) } else { $psISE.PowerShellTabs[$i].Files.Add($p) | Out-Null } $found ++ break } } if( $found -eq 0 ) { Write-Error "Could not find a tab named "" $TabName """ } continue } # Add the file using tab number if( $TabNumber ) { if($AsCopy) { $content = Get-Content $p -Raw $newFile = $psISE.PowerShellTabs[$TabNumber].Files.Add() $psISE.PowerShellTabs[$TabNumber].ExpandedScript = $true $newFile.Editor.Text = $content $newFile.Editor.SetCaretPosition(1,1) } else { $psISE.PowerShellTabs[$TabNumber].Files.Add($p) | Out-Null } continue } if($AsCopy) { $content = Get-Content $p -Raw $newFile = $psISE.CurrentPowerShellTab.Files.Add() $newFile.Editor.Text = $content $newFile.Editor.SetCaretPosition(1,1) } else { $psISE.CurrentPowerShellTab.Files.Add($p) | Out-Null } } } End{} } #endregion #region Remove-PSISETab Function Remove-PSISETab { <# .SYNOPSIS Remove a powershell tab. .DESCRIPTION Remove a new powershell tab awithin ISE. .PARAMETER Name The name of the tab. .EXAMPLE Remove-PSISETab -Name MyTab This command will remove the MyTab tab. .EXAMPLE Remove-PSISETab -Current This command will remove the current tab. #> Param ( [Parameter(ParameterSetName="TabName", Mandatory=$True, Position=0)] [string]$Name, [Parameter(ParameterSetName="TabNumber", Mandatory=$True, Position=0)] [int]$Number, [Parameter(ParameterSetName="TabCurrent", Mandatory=$True, Position=0)] [switch]$Current, [Parameter(ParameterSetName="TabName", Mandatory=$false)] [Parameter(ParameterSetName="TabNumber", Mandatory=$false)] [Parameter(ParameterSetName="TabCurrent", Mandatory=$false)] [switch]$Force ) # Get the current tab if( $Current ) { $tab = $psise.CurrentPowerShellTab } # Get the tab using the number if( $Number ) { if( $Number -ge $psise.PowerShellTabs.Count ) { Throw "No such tab!" } else { $tab = $psise.PowershellTabs[$number] } } # Get the tab using the name if( $Name ) { $found = 0 # Search for the tab for( $i = 0; $i -lt $psise.PowerShellTabs.Count; $i++) { if( $psise.PowerShellTabs[$i].DisplayName -eq $Name ) { # Get the tab $tab = $psise.PowerShellTabs[$i] $found++ } } # Check if a tab with the specified name exists if($found -eq 0) { Throw ("Could not find a tab named " + $Name) } } # Check for unsaved files $unsavedFiles = @($tab.Files | Where-Object {$_.IsSaved -eq $false}) if($unsavedFiles.count -gt 0) { if($Force) { # Save the files to %temp% and then remove them foreach($f in $unsavedFiles) { $tempFile = [System.IO.Path]::GetTempFileName() $f.SaveAs($tempFile) Remove-Item -Path $tempFile -Force } } else { Throw ("The tab " + $tab.DisplayName + " has unsaved files.") } } # Close the tab $psise.PowerShellTabs.Remove($tab) | Out-Null # if the current powershell tab was closed, switch to the first tab if($Current) { $psISE.PowerShellTabs.SetSelectedPowerShellTab($psISE.PowerShellTabs[0]) } } #endregion #region Exports Export-ModuleMember -Function New-PSISETab Export-ModuleMember -Function Rename-PSISETab Export-ModuleMember -Function Open-PSISEFile Export-ModuleMember -Function Remove-PSISETab #endregion |