We will learn how to Create a File Template & Open With VSCode. This can be used for more than PowerShell. For any file that has text data, this process will work. Let’s begin talking about automation. This is how each one of my PowerShell scripts looks. I like this look. Here is the final result.
I cannot be the only one to come to this conclusion. At some point, you write, or start writing many, many scripts. Some are one line, some are much much bigger. In either case, copying and pasting relevant data into each newly created script is tedious and time-consuming. Yes. I completely understand that the process only takes a few seconds of manual input. I see it as a much bigger issue. Let me explain. The end goal is to type in one command and have a “default” PowerShell script ready to go.
Say the process takes 30 seconds. To create a file, add relevant comments to the file and have said file saved to a directory of your choice. Now, do this process six times a day. That is already three minutes per day you spend just creating new script files. Then multiply that by 252ish days (working days per year). That’s 756 minutes a year or 12.6 hours.
That is one full day for me. That is including travel time to and from work. There are so many other things I can do within those three minutes a day. That’s what we are working on today. To start, we will build an advanced function. This way we can pass variables to it at runtime when we call the function.
We start by naming the function. We then use CmdletBinding()
functions. These functions specify the CmdletBinding
attribute can use the methods and properties that are available to compiled cmdlets. This lets us define the parameters we want for this function. Specifically, we want to not only require the -Name
but also let that variable data pass to and from the function.
There is a lot about advanced functions we will not get into right now. However, if this interests you please leave us a comment. In the meantime check out the Microsoft Documentation. Next, we will define the variables.
Above we define the $location
variable and tell it to create a file with $name.ps1
. This is directly why we needed the advanced function. So when we call the function with -Name
modifier we can name our file whatever we want.
Next is the $code_path
variable which houses the location of Visual Studio Code’s .exe
file so we can open it later.
We define the $date
variable using the Get-Date
cmdlet and a bit of dot(.
) sourcing .ToString
and the definition as I want to see it. M/d/y
.
We want to preserve the formatting of the $data variable. When defining the $data variable we use what is known in PowerShell as a here-string. Like most other high-level programming or scripting languages, PowerShell gives us the ability to perfectly preserve line breaks, whitespace, tabs, and other text formatting with the here-string.
To define a here-string in PowerShell, you need to use the @ character followed by either a single or double quote and then a newline. To end, close the quotes and add another @ on a newline, so it looks like:
@" "@
Now we can start on the three lines we need to complete this, and make this happen.
The New-Item
cmdlet creates a new item and sets the value of that new item. The type of items that can be created using New-Item
depend partially, on the location of the item. For example, in the file system, New-Item
creates files and folders. In the registry, New-Item
creates registry keys and entries. Yes, it can add content when it makes the file, however, I had issues with this working (Probably my skill level) in the way I expected so we took a different approach.
Using Add-Content
cmdlet we now get to use the $location
variable, and the $data
variable to add the preformatted text to the .ps1
file we just created. Add-Content
adds data (content) to the specified item(s), such as adding text like we are doing to a file.
Lastly, we use Start-Process
to call the -FilePath
variable for VSCode, then -ArgumentList
is the $location
variable. Start-Process
starts processes on the local computer. By default, Start-Process
creates a new process that inherits all the environment variables that are defined in the current process. This is important information, the last sentence there. We will not use it today, but it good to know.
That is all there is to it. Below you will find the script in its entirety.
Check our other PowerShell scripts below: