Could you look over my script?

Hi there, I’ve recently started using Powershell and by recently I mean something needed automating and I googled until I found something that works on my system. Long story short, the senior IT guy I report to doesn’t know powershell but they asked me to find a way to automate some tasks.

Before I go posting a bunch of code I wanted to ask would it be terribly inappropriate to beg you to look it over? I’ll remove all the sensitive stuff of course but I’d like someone with a bit of experience to look at it and make sure its not a complete atrocity.

We are here to help Nick.

Place your question, then we can look at it.

Alright, well this is to check for a process, if its not running, start it, and in one case, automatically log into the software. I’ve managed to cobble this together by googling and its my first effort at doing anything at all in PowerShell. On the plus side, it works exactly as I want it, I was just wondering if there are any possible undesirable effects this might have.

You’ll have to forgive the mass of sendkeys, there was no other way of getting it to run the program via a launcher. If I ran the executable directly it would start but be unresponsive to input so I was forced to navigate the launcher and start the software from there.

$process = "TheProgram"
$check = Get-Process $process

if(!($check)) {

add-type -AssemblyName System.Windows.Forms

$encrypted = get-content C:\cred.txt
$user = "UserName"
$Password = ConvertTo-SecureString -string $encrypted
$cred = New-Object -TypeName System.Management.Automation.PSCredential -argumentlist $Password
ConvertFrom-SecureString $Password
$NonSecure = [Runtime.InteropServices.Marshal]::PtrToStringAuto([Runtime.InteropServices.Marshal]::SecureStringToBSTR($Password))

#Unfortunately I didn't know of a way to make powershell interact directly with the software, the provider "doesnt support powershell" and wouldnt help me
#So the below is a set of keystrokes with as little time as possible between them to allow for windows to load and such

Invoke-Item "C:\Program Files\TheFile\WithTheProgram\LauncherExecutable.exe"
start-sleep -Seconds 2

Start-Sleep -Seconds 1

Start-Sleep -Seconds 1

Start-Sleep -Seconds 8

Start-Sleep -Seconds 10


#With an email message to the IT Dept stating that the program needed relaunching

Send-MailMessage -From "The Automated PC" -To "IT Dept" -Subject "TheProgram has been restarted" -Body "TheProgram was not running on MachineName and has been restarted." -SMTP ""

#Then more of the same with outlook
#Although the $check variable is identical to above the script broke if it wasn't redefined here for the next section though I have no idea why

$process = "outlook"
$check = Get-Process $process
$executable = "C:\Program Files (x86)\Microsoft Office\Office14\outlook.exe"

if(!($check)) {
Start-Process $executable

Send-MailMessage  -From "The Automated PC" -To "IT Dept" -Subject "Outlook was restarted" -Body "Outlook was not running on MachineName and has been restarted." -SMTP ""


I’m not any means expert on SendKeys so I don’t comment on that, but I would recommend to save and get the password otherwise. Seems that the password is now in plain text in that .txt file.

Also if you want to “save some time” move the $executable inside the if so it would only be executed when needed. Also I would change all “Outlook” text to $process so when you change $process you don’t need to type those into the subject and body etc.

One approach to this could be also Try Catch Finally, where you could try if it is running then do what you need to do and in Catch you’ll do what you’ll need to do when it is not running and if needed add Finally where you could always send mail/make log files what has happened. Of course if mail send is only wanted when it is not running then add it to Catch and skip Finally, or make logging there has it been running or not.