Visual Studio Powershell

Posted on  by 



Tools

Customizing Visual Studio for PowerShell users

Visual Studio Code: Microsoft's free, lightweight, and open-source text editor with the PowerShell extension that offers a full PowerShell development experience. A PowerShell console: Debug using the same commands you would use to debug any other PowerShell process. The key to opening PowerShell Core and PowerShell for Windows side by side is the Shell Launcher Visual Studio Code extension. Shell Launcher allows you to configure multiple shells within Visual Studio Code. Install the extension by clicking on the Visual Studio Code extensions button and searching the market place for shell launcher. Integrated Terminal. In Visual Studio Code, you can open an integrated terminal, initially starting at the root of your workspace. This can be convenient as you don't have to switch windows or alter the state of an existing terminal to perform a quick command-line task.

Setup and get key To publish your PowerShell module, create an account in PowerShell Gallery and obtain API Key. Go to PowerShell Gallery and click 'Register' Register. PowerShell – How to build a GUI with Visual Studio Sometimes PowerShell scripts are a way to comlicated to present your hardly gathered data to end users that are not familiar with the command line. Therefore I recommend to present data by Windows WPF forms that could be.

Why?

I find this a very useful customization to Visual Studio to run project specific tasks from PowerShell while developing. For example I have PowerShell .ps1 scripts in a Tasks or Deployment solution folders that do things like:

  • Refresh nuget packages because I use nuget without committing packages (Update: Nuget.org have finally fixed this!)
  • Seeding my Database/ RavenDb Document stores
  • Testing my Build/ Automated test scripts outside of CI

It’s nice to be able to right-click them (or use a shortcut key) to execute them in place just like this:

So on to how to set it up:

Visual Studio Powershell

##

Step 1: Adding “run powershell script” as an external tool

  1. In Visual Studio go to the menu: ToolsExternal Tools
  2. Click the “Add” button
  3. Add the following form values:
    • Title: “Run Powershell script in output window”
    • Command: “C:windowssystem32windowspowershellv1.0powershell.exe”
    • Arguments: “ -file “$(ItemPath)”
    • Initial Directory: “$(ItemDir)”
    • Tick “Use Output window”
    • (Close on exit will now be automatically on)
  4. Click the “Apply” button
  5. Click the “Add” button
  6. Add the following form values:
    • Title: “Run powershell script outside of VS”
    • Command: “C:windowssystem32windowspowershellv1.0powershell.exe”
    • Arguments: “ -file “$(ItemPath)”
    • Initial Directory: “$(ItemDir)”
    • Don’t tick “Use Output window”
    • Tick “Close on exit”
  7. Click the “Ok” button

They should look something like this:

Step 2: Weird Step, trust me!

Check the index position it is in the external tools list. By default mine are at positions 6 and 7. (I think by default Create GUID is no. 1!)

Step 3: Hook it up to the context menus

Visual Studio Powershell Sdk

  1. Go to the menu: ToolsCustomizeCommands
  2. Click the “Context menu” radio option
  3. Scroll down to “Project and Solution Context MenusItem” (nightmare long menu, type “Proj” to get roughly to the right place)
  4. Click the “Add Command” button
  5. Select the category: “Tools” and Command: “External Command 7” (or whatever your position is you got from the “Weird Step 2”)
  6. Hit the “Ok” button
  7. Then to set up the 2nd command:
  8. Select Category: “Tools” and Command: “External Command 8” (or whatever your position is for the other one)
  9. Hit the “Ok” button again
  10. Move them around till you are happy with their order (I usually put them somewhere below “Open With…”)

Step 4: Add your keyboard shortcuts

  1. Go to the menu: ToolsOptions
  2. Select the EnvironmentKeyboard section
  3. Find the Tools.ExternalCommandN item in the list (nightmare long list again, type “Tools” to get you roughly there again)
  4. Select your shortcut key for each command: I like <CTRL> <SHIFT> <P> and <CTRL> <SHIFT> <ALT> <P> respectively

You are all done, enjoy your PowerShell efficiency!

##

Visual Studio Powershell Project

What kind of PowerShell scripts do you find useful to run straight from Visual Studio?

Sorry, we run comments powered by Disqus which require JavaScript. Alternatively you could submit it to hacker news to comment on it?

Sometimes PowerShell scripts are a way to comlicated to present your hardly gathered data to end users that are not familiar with the command line. Therefore I recommend to present data by Windows WPF forms that could be achived without being a coding expert.

With the help of Visual Studio everyone is able to build WPF forms even without coding expertise. In this tutorial I will show you how to generate a User Information script displaying Hostname, Username and Domainname. This is a basic tutorial showing how to build a graphical user interface within minutes. The script can be extended easily, for example you can display “Model Type” of your Hardware, “Network Drives” connected, “Outlook Profile” size, installed software and a lot more!

VisualStudio can be downloaded for free at Microsoft – https://www.visualstudio.com/downloads/

Start a Project

Start Visual Studio (Express / Community / Professional) and open a “New Project …

Result:

The result is an empty WPF form that will be extended within the next steps.

Add Labels, Textboxes and Close Button

Add four labels to your form as in the picture below. If the Toolbox on the left side isn’t available on your Visual Studio installation you could reopen this element by clicking “View –> Toolbox“.

Add a close button to your form.

Result:

Visual Studio will automatically generate the following XAML code that can be used in PowerShell.

To generate a WPF in PowerShell we can use the following script as a good start.

Studio

Before we can paste our Visual Studio XAML code into the PowerShell script we have to perform some small modifications:

  • Change line 1 from<Window x:Class=”WpfApplication9.MainWindow”to <Window
  • Remove line 6 and 7
  • Line 10 to 17 – Renamex:Name=to Name=

After modification just paste the XAML code to your PowerShell script in line 4.

Result:

Save your PowerShell script and execute it. If you used PowerShell ISE to built your script just press F5 and you will see the following result.

Congratulations you built your own PowerShell GUI!

Add Close Event

Now that we are able to display our form we should get the close button running first. We just have to assign a event to our button that is named “button” by default.

Visual Studio Powershell Gui

You are now able to close you application by clicking

Visual Studio Powershell

Visual studio powershell guiVisual studio powershell

Add System Variables

To receive the Hostname, Username and the Domain we can use the following commands:

Just add the following marked lines to your code and you are done with your first PowerShell WPF Form.

Visual Studio Powershell 7

Result:

Feel free to modify your XAML code in Visual Studio to get better user experiences. As an example I’ve created the following XAML code.

As you can see the result looks different to the form before and it just took a few minutes to achive this result in Visual Studio.





Coments are closed