Tome's Land of IT

IT Notes from the Powertoe – Tome Tanasovski

Category Archives: ISE

Open a file in PowerShell ISE via cmdlet – Version 3 Update

A while back I posted an article that discusses a cmdlet I created that opens up text files in powershell ISE.  It’s fairly robust: It accepts pipeline, wildcards, handles multiple files, etc.  I was just transferring my profile over to my Windows 8 Server computer, and I decided to revisit the script for PowerShell ISE in version 3.0 of PowerShell.

If you are not aware, powershell_ise.exe now accepts a new parameter called -File :

powershell_ise.exe -help

The way that -File works is better than I expected.  It not only opens up a PowerShell ISE window, and then opens the file in it, but it will inject the file into a PowerShell ISE window if it is already open.  Here is the relevant bit of code to launch powershell_ise.exe from a powershell.exe host.

# $files is an array that contains the full path to every file that will be opened
start powershell_ise.exe -ArgumentList ('-file',($files -join ','))

You can download the full script on PoshCode.

Advertisements

V3 ISE Colors

I have a love/hate relationship with the new ISE.  I’ll spare any discussion on the topic until after the CTP is no longer a CTP.  In the meantime, I’ll just leave you with a quick script to help you if you are as resistant to change as I am with my beloved ISE.

In the new ISE the output pane and command pane are one.  They happen to look like this:

For my eyes, I prefer the go-lightly look of the v2 ISE.  If you feel the same way, you can have everything you want by running the following script:

$psise.Options.OutputPaneBackgroundColor  = "#FFF0F8FF"
$psise.Options.OutputPaneForegroundColor = "#FF000000"
$psise.Options.OutputPaneTextBackgroundColor = "#FFF0F8FF"

foreach ($key in ($psise.Options.ConsoleTokenColors.keys |%{$_.tostring()})) {
    $color = $psise.Options.TokenColors.Item($key)
    $newcolor = [System.Windows.Media.Color]::FromArgb($color.a,$color.r,$color.g,$color.b)
    $psise.Options.ConsoleTokenColors.Item($key) = $newcolor
}

Now doesn’t that look better – I mean more familiar 🙂 The above applies the script pane’s token colors to the command pane. Token colors are new to the command pane in V3.

Two additional notes:

1) If you want to restore back to the original look and feel that was shipped, you can use the following:

$psise.Options.RestoreDefaults()
$psise.Options.RestoreDefaultConsoleTokenColors()

2) Everything in this article can now be done in ISE via their new color themes, but again let’s wait to see what happens during RTM.  However, you can play with it yourself for now by clicking Tools->Options…  and then clicking the Colors and Fonts tab in ISE.

Open a file in Powershell ISE via cmdlet

Today I found myself annoyed at the process I have to go through to open a script while working in ISE.   I decided to put together a quick cmdlet that will make this very easy.  I used the technique from my last post to ensure that I could pipe in a collection of scripts to open at once.  Perhaps I’ll grow this module with time, but for now you can download it with the single cmdlet here (Make sure to unblock the .zip prior to extracting).  I’ve also uploaded the code behind the function to Poshcode.

Here is the contents of get-help open-ISEFile -full:

NAME
    Open-ISEFile

SYNOPSIS
    Open a new file in ISE

SYNTAX
    Open-ISEFile [-Path]  [-PassThru] []

DESCRIPTION
    This cmdlet allows you to open a file in a new file tab within your current Powershell t
    ab.  You can pass a collection of files to open more than one file.

PARAMETERS
    -Path 
        Specifies a path to one or more files.  Wildcards are permitted.  The default locati
        on is the current directory (.).

        Required?                    true
        Position?                    1
        Default value
        Accept pipeline input?       true (ByValue)
        Accept wildcard characters?  

    -PassThru []

        Required?                    false
        Position?                    named
        Default value
        Accept pipeline input?       false
        Accept wildcard characters?  

        This cmdlet supports the common parameters: Verbose, Debug,
        ErrorAction, ErrorVariable, WarningAction, WarningVariable,
        OutBuffer and OutVariable. For more information, type,
        "get-help about_commonparameters".

INPUTS
    System.String

OUTPUTS
    None or Microsoft.PowerShell.Host.ISE.ISEFile

    When you use the PassThru parameter, Open-ISEFile returns a Microsoft.PowerShell.Host.IS
    E.ISEFile for each file opened.  Otherwise, this cmdlet does not generate any output.

NOTES

        Name: Open-ISEFile
        Author: Tome Tanasovski
        Created: 6/20/2010
        Version: 1.0

    -------------------------- EXAMPLE 1 --------------------------

    C:\PS>Open-ISEFile -Path $profile

    Opens your profile in ISE.

    -------------------------- EXAMPLE 2 --------------------------

    C:\PS>dir *.ps1 |Open-ISEFile

    Opens up all ps1 files in the current directory as new file tabs in ISE.

    -------------------------- EXAMPLE 3 --------------------------

    C:\PS>Open-ISEFile *.ps1

    Opens up all ps1 files in the current directory as new file tabs in ISE.

    -------------------------- EXAMPLE 4 --------------------------

    C:\PS>$file = Open-ISEFile "c:\file1.ps1" -PassThru

    Opens up file1.ps1 in ISE.  The command uses the passthru parameter to generate an objec
    t that represents a file in ISE.

RELATED LINKS
     https://powertoe.wordpress.com
%d bloggers like this: