Hello, here’s a quick tutorial on how to make your computer type for you in PowerShell on Windows.

Have you ever needed to type something on a computer?

Over and over.

And maybe you did not want to repeat that task.

This script is for you.

Making your computer type for you is the usual first step to learning how to make your computer do your work.

Also, the first step into getting started with UI automation.

What is UI AUTOMATION? An entire world of easy tools, that make your life easier the more you learn.

The goal here is you want to learn how to automate something that you are doing manually.

This kind of automation should be used to put yourself out of work so that you can get your life back with your family.

You can open PowerShell ISE, it’s a code editor.

Powershell ISE is a free app to help you develop code in Powershell. AKA Posh.

Powershell ISE is a free app to help you develop code in Powershell. AKA Posh.

Or open a text file and save the following code:

$k= New-Object -com "Wscript.Shell"
$a=1
$b=2
$c="Meow moew"
Start-Sleep -seconds 5
$k.sendkeys("$c")

Save as a .ps1 file.

Hit play, or double click the file to execute.

It will take 5 seconds, then your computer will type Meow Meow.

If you kick off any automated security alerts, work with your IT department to unlock this ability. You are building a new app and that means it can do positive or negative things. Which is a risk from a commercial/enterprise business computing perspective. Also, data privacy, your manager, smart people in your company will likely want to know what the code looks like and will seek to read the code before they let it play in their environment. Or you’re at home and no big deal.

Line by line tutorial below:

Line 1
$k= New-Object -com "Wscript.Shell"

New-Object Creates an instance of a Microsoft .NET Framework or COM object.

New-Object Opens the ability to send a keystroke within PowerShell.

We call this with $k moving forward.

Like in math class, you used X to represent a value or Y to represent a value.

This makes our code easier to write and read.

$k.sendkeys, later in the script, is now easier.

-comobject or -com for short is a special ability.

The -Comobject Specifies the programmatic identifier (ProgID) of the COM object. Luckily you don’t need to understand what this means.

Line 3,4,5
$a=1
$b=2
$c="Meow moew"

Like in math class, you have X representing a value and Y representing a value.

We will use these extra variables later in the tutorial.

Today, $a and $b represent a tiny lesson, which is.. you can leave unused code.

And if you remove it, your code will be faster.

Line 7
Start-Sleep -seconds 5

Ever tried living without sleeping? Writing UI automation is the same. You need breaks, waits, pauses, etc.

In powershell, it’s called Start-Sleep -seconds X…

Optimize your code by updating the following.

$k= New-Object -com "Wscript.Shell"
$a=1
$b=5
$c="Meow moew"
Start-Sleep -seconds $b
$k.sendkeys("$c")

Swapping values visually in paint using powershell

Paint helps quickly explain where these two swap. Helping us apply another X value to a number.

explaining basic stuff with screenshots in powershell

Here’s how we use $b later in the lesson. Glad you’re not skipping ahead! Powershell isn’t half bad, right?

Line 8
$k.sendkeys("$c")

Finally, let’s make my computer say meow meow.

You’re passing the $c = “Meow meow” to whatever text field available.

explaining UI automation by automating text into my twitter handle @itylergarrett

Meow meow on twitter.. Great. 0 likes.

Interested in more?

How to Loop your Code | PowerShell Looping

Cool, I’m sure I will write about how to do this in advanced versions but without the basics, I doubt anyone will understand where this will go!

Best,

Tyler Garrett