Chris Rasmussen · Infrastructure Guy · Code Dabbler · Photographer · Traveller

PowerShell – Optimised Applications

I’m pretty keen on finding ways to keep my system optimised. So much so that I often break stuff on my machine trying various things but hey, you can’t make an omelette if you don’t break a few eggs. This post is about writing a Windows Powershell script that starts the services related to a selected application, starts the application, waits for the application to finish then stops the services again. It’s pretty simple in concept really but the intention is that it will keep your system’s memory footprint as small as possible.

It’s worthwhile noting at this point that there’s not much point using this script to start applications that don’t have any related services. You’ll also need administrative privileges to perform these operations. I know that a lot of systems these days have loads of RAM (my laptop has 4GB) but with technical people often installing many applications that they don’t need all at the same time, this can help keep things smooth.

To follow through this you’ll obviously need Windows PowerShell installed. A previous post of mine called "PowerShell – Why won’t my scripts execute?" covers a common problem people run into after they’ve installed PowerShell so head on over there if you’re new to things.

This post also makes use of some stuff I wrote in another recent article called "PowerShell – Checking Windows services status" so please feel free to click that link if you aren’t familiar with managing Windows services with PowerShell.

For this example the script is going to be starting the services for VMWare Workstation, starting the VMWare Workstation application then stopping the services afterwards. The version of VMWare Workstation I’m running has 4 services that we’ll be playing with. These are:

VMware Authorization Service
VMware DHCP Service
VMware NAT Service
VMware Virtual Mount Manager Extended

To start services in Windows PowerShell you need to make use of the Start-Service cmdlet as you’ll see in a minute. Naturally to stop the services you use the Stop-Service cmdlet. If you’re REALLY new to Windows PowerShell please note that you can’t run these commands from a normal command prompt window – you must be running Windows PowerShell itself (yes, someone asked me about that recently so I feel like I should clarify that point).

I’m going to assume you’ve read my previous posts about starting and stopping services so without further ado, here’s the script in its entireity. The comments I’ve put throughout the script should make it obvious what each line does.

Please ignore the fact that the code excerpt below is labelled as C# – the code highlight plugin I use unfortunately doesn’t support PowerShell syntax yet.

?View Code CSHARP
# Windows Powershell script to start VMware Workstation 6.0.2
# Starts the required services, starts the application without closing the script window then stops the services
# All in the name of optimising system performance by minimising the number of services that run when you don't need them
#
# Note that this script has been written for a system that runs VMware Workstation 6.0.2
# It will also work any other version of VMware Workstation that uses the same services
#
# Chris Rasmussen, March 2008
 
# be tidy - clear the screen
cls
 
# start the required services
# note that none of these processes have any dependencies so it doesn't matter what order this is done
write-host "Starting services required for VMware Workstation 6.0.2 operation ..."
start-service -displayname "VMware Authorization Service"
start-service -displayname "VMware DHCP Service"
start-service -displayname "VMware NAT Service"
start-service -displayname "VMware Virtual Mount Manager Extended"
write-host ""
 
# setup the variables for the required VMware executables
$vmwarePath = "C:\Program Files (x86)\VMware\VMware Workstation"
$vmwareExecutable = "vmware.exe"
$fullExecutable = "$vmwarePath\$vmwareExecutable"
 
# start VMware Workstation
write-host "Starting VMware Workstation ... do not close this window until you have correctly closed VMware Workstation!"
write-host ""
$vmwareProcess = [diagnostics.process]::Start("$fullExecutable")
$vmwareProcess.WaitForExit()
 
# now that VMware Workstation has closed, stop the services that aren't needed anymore
write-host "Stopping VMware Workstation services ..."
stop-service -displayname "VMware Authorization Service"
stop-service -displayname "VMware DHCP Service"
stop-service -displayname "VMware NAT Service"
stop-service -displayname "VMware Virtual Mount Manager Extended"
 
# be nice and informative at the end
write-host ""
write-host "All done!"
write-host ""

To summarise, the script does the following things, in order.

- Starts the services required for VMWare Workstation
- Starts the VMWare Workstation application
- Waits for VMWare Workstation to stop
- Stops the VMWare Workstation services

Please note that if you want to use the script above and haven’t installed VMWare Workstation into the same folder as me you’ll need to change the paths to point to the appropriate location on your system. I’m running Windows Vista Ultimate x64, hence the Program Files (x86) installation directory.

You can click here to download the complete script above in .ZIP format.

The next step is to create a shortcut to your script that you run in the normal way – with a mouse click. The shortcut’s properties should point to the following (change the location of your .ps1 script as necessary).

C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe “c:\Data\Scripts\Powershell\vmware-workstation.ps1″

During the creation of the shortcut you will need to click the Advanced button and enable the option named "Run as administrator". Note that if you are running Windows Vista and UAC enabled this will force Vista to prompt for confirmation that you want to continue running the application as administrator. The screenshot below shows this option.

Windows Vista - Run Shortcut As Administrator

The shortcut’s default icon will be the icon for Windows PowerShell so you will want to change the icon to be the same as the application’s executable file icon. For example …

PowerShell default icon:
Icon - PowerShell

One of the VMWare icon’s available if you choose vmware.exe as your icon repository:
Icon - VMWare

Pretty simple but a good way to keep your system optimised. I’ll write another post soon about what other things I’ve done to tune Windows Vista – even with 4GB RAM my system is noticeably faster than before.

  • Share/Bookmark

Related posts:

  1. PowerShell – A note about using .NET & PowerShell together I should’ve mentioned this in a previous post but I...
  2. PowerShell – Checking Windows services status As I said in my previous post, PowerShell – Why...
  3. PowerShell – Why won’t my scripts execute? Microsoft Windows Powershell is becoming more and more popular now....
  4. PowerShell – How do I install it on Windows Server 2008? Unlike Windows XP, Windows 2003 and Windows Vista, Windows PowerShell...
  5. What tools, software & applications do I use? This post is intended as a couple of things. Firstly,...

banner ad

Leave a Reply

Powered by Wordpress | Designed by Elegant Themes