PowerShell scripts for developers

PowerShell is an amazing framework when it comes to administrating and automating administrative tasks. It consists of a command-line shell and associated scripting language built on the .NET Framework. PowerShell can also increase efficiency of supporting and working in .Net environments.

There are few PowerShell scripts I have been using repeatedly to ease day to day development tasks. Each of this script is available one click away for direct execution on my workstation. There are some scripts which I utilize almost every day and there are other I use when I want to quickly inspect an environments.

In this section I will share some of these scripts from my PowerShell vault. Here is list of different tasks we will try to solve using PowerShell in this post:

  • Get Process Ids and Names of Worker Processes
  • Update user identity password on all Application Pools
  • List Website to Application Pool association
  • Get Application Pool and their properties
  • Find all installed .Net Framework versions
  • Add user to Administrators group
  • Get certificate thumbprint

Before we begin, let’s look at basic setup you will need to get started.

Setup

If you have never used PowerShell before, note that it’s locked down from running scripts by default. You will need to run PowerShell as Administrator once and run following script:

By default PowerShell has “execution policies” setup as Restricted to support secure command line administration.  Execution policies define the restrictions under which PowerShell loads files for execution and configuration.  The four execution policies are: Restricted, AllSigned, RemoteSigned, and Unrestricted.

One other useful command you should know before you start is Get-Help. You can use this command to get help with any other command. For example, if you want to know how the Get-Process command works, you can type:

If you are working with IIS (Internet Information Service) then you will need the Web Service Administration Module which includes cmdlets to manage configuration and runtime data of IIS. It implements a namespace hierarchy containing Application Pools, Web sites, Web applications and virtual directories.

Running above command successfully will add a new drive for IIS, which you can verify by running Get-PSDrive

Get Process Ids and Names of Worker Processes

If you have .Net Web Apps deployed in IIS then you would have run into issue of finding out which worker process (W3WP) is associated with which Application Pool.

PowerShell_TaskManager

There are few ways of finding out worker process to Application Pool association, the most straight forward being IIS > Worker Processes tab. However, there is an easier way than opening the IIS and finding the right tab. You can run following 3 lines as PS1 file and keep that file handy so that you can right click on chose Run with PowerShell to find the matching Application Pools quickly.

For the Task Manager Screenshot above here is what is returned from PowerShell:

PowerShell_AppPool_ProcessIDs

Tip: The last line ‘$host.UI.RawUI.ReadKey’ will ensure that the PowerShell window does not close before you have seen the result.

Update user identity password on all Application Pools

At CMC we have password policy which enforces password update on a fixed interval (feels like every day). For me, most of the application pools on local machines are running under my identity, meaning I have to update password of each and every Application Pool when I change my domain password. Now, instead of updating password for each Application Pool manually I use following PowerShell script to update all Application Pool.

Note: If you are pasting above commands to PowerShell window rather than running them as PS1 script then you should run lines: 1-6 first and then rest of the lines.

List Websites to Application Pool association

I run into need for this task often when I am debugging in client environments and when I am trying to find out Application Pool to Web Site association. Following simple command will return list of Application Pool with Web Site association.

Tip: OGV stands for Out-GridView. The Out-GridView cmdlet sends the output from a command to a grid view window where the output is displayed in an interactive table.

The result with OGV option shows up as this –

PowerShell_OGV

Get Application Pool and their properties

Similar to previous command, this command is quite useful when working in unknown environment. You can run this command to get quick information on different Application Pool on system you are working with. Of course there are alternate ways available of going in to IIS and inspecting Application Pools, but I suspect you would ever use them once you have following script readily available.

Find all installed .Net Framework versions

Following command will list all the installed .Net Framework versions on machine you are working with.

Add user to Administrators Group

Even though this is an IT Admin kind of script, I use it quite often to add myself or others as Admin in different environment. This 2 lines can prevent you from going into Server Manager and few other clicks just to grant user admin rights. In sample below I am adding my username as admin in local machine. This command can only be run under context of an Admin.

Get certificate thumbprint

I use this command on rare occasion but whenever I need thumbprint of locally installed certificate I rely upon this command. This command ensures that there are no invalid character or white spaces in thumbprint when I copy it from the result.

More

Scripts presented in this blog should act as starting point for your personalized versions. There are tons of resources available on web showing full capability of PowerShell. Here are some resources if you would want to take PowerShell to next level:

-tushar ~tas

Leave a Reply

Skip to toolbar