5 steps which help you to write your first Powershell script

5 steps which help you to write your first Powershell script

Hi, I’m Damian Garbus. It’s great to have you here. It’s the first post in the 2019 year. Did you set goals on the new year? I did this. Now it’s time to replace the plans for tasks and complete them!!! OK, but this post is not about my 2019 goals. I want to show you the first way to write your first script and run it. I wrote many blog posts about Powershell basics, and now it’s time to use them in your first Powershell script. Don’t worry. I will still write new posts about Powershell for newbie and Powershell basics for everyone. However, It’s time to use knowledge from my blog and practice, practice and one more time practice.

Where to start

If you want to write your script, you have to know what information You want to insert to script and what information this script should return for you. I will show you a simple example of how to start writing the Powershell script but more important is an idea before starting writing scripts. In this post, I will only show you how to create Powershell scripts file and fill out this with Powershell commands, parameters, and functions. After this, you can test the script. All required steps I will show you using Visual Studio Code.

Powershell script it’s file with “*.ps1” extension

5 steps to write Powershell script

Create ps1 file

Before you start, you have to create your script file. It’s a file with “ps1” extension. In Visual Studio Code you can create not only file but directory for these scripts.

write powershell script

Write param block

About param block, I wrote in the post Why Powershell param block will change your scripts to better, so please read this article if you don’t know what is param block now and come back later. The param block isn’t required to run the Powershell script. On the other hand, all the Powershell scripts should contain this. Ok, so it’s time to add param block to your first script!

write powershell script

Write your script

Now you can write your main tasks in the Powershell script. Write everything that Powershell script has to do step by step. During Powershell script writing, you should fill out param block with variables which should be available to change when the script is invoking.

[EDIT] Before you add Powershell commands to script, always you can check this command in Powershell console if it works fine. If script will not working, this approach help you troubleshooting what doesn’t work.

write powershell script
write powershell script

Add required function or write new for repeated tasks

If you repeat a few sequences of command, probably you can change them to Powershell function with others param block. After doing this, you can invoke this function by name as a Powershell command and all repeated sequence will be run. One more advantage is that, if you want to change this sequence of commands you can do change only in function, but not in all places in Powershell script.

write powershell script

Test in Powershell console

In the end, You can run the script and test it. It can be done in Visual Studio Code but also in Powershell console.

write powershell script


Above, I showed you 5 steps to write Powershell scripts. I want to tell you one more time, that the most important is an idea to write a good Powershell script. For high-level scripts I’m using ‘Coggle.it’ to draw Powershell script schema before I will create the script file. More about the tools I’m using you can read here.

Now you know how to start, so please practice and test your script. If you have any problem or you want to share your success first script, please write in comments

Get Access to Poshland Resources for FREE!!!

Signup now and receive an email once I publish new content.

I agree to have my personal information transfered to MailerLite ( more information )

I will never give away, trade or sell your email address. You can unsubscribe at any time.