Monday, November 11, 2013

Open an .HTML file with PowerShell

Have you ever wanted to open a .HTML file with PowerShell, either in a script or just because you were already in a PS console? There is actually a really easy way to do this in your default browser via PowerShell. The cmdlet "Invoke-Expression" will run the any command or expression.

Lets take a look at Invoke-Expression's help file by running the command help Invoke-Expression -full I prefer to run the cmdlet Help instead of the full cmdlet Get-Help because help will only show you a page at a time, so by default typing help is the equivalent to Get-Help command | more
PS []> help Invoke-Expression -full


    Runs commands or expressions on the local computer.

    Invoke-Expression [-Command]  []

    The Invoke-Expression cmdlet evaluates or runs a specified string as a command and returns the results of the
    expression or command. Without Invoke-Expression, a string submitted at the command line would be returned
    (echoed) unchanged.

        Specifies the command or expression to run. Type the command or expression or enter a variable that
        contains the command or expression. The Command parameter is required.
In order to open an .HTML file from PowerShell you must first know the location of the file, then simply type, "Invoke-Expression .\filename.html"
PS []> Invoke-Expression C:\Users\mkerfoot\Desktop\HTML_report.html
Invoke-Expression can also be used to open an image or any filepath applicable.
PS []> Invoke-Expression C:\Users\mkerfoot\Desktop\Icon.PNG
The Following command will open up the file I specified in your default browser.