You are on page 1of 1

13

Figure 6: A file path is stored in the $filePath variable. The New-Item cmdlet is
used to create a new file to the path specified by the $filePath variable.

Some variables are created automatically by PowerShell. Automatically created


variables store data about the state of the PowerShell session. Their values can-
not be set by the user, PowerShell updates their values as needed. (Microsoft
2017m.) For example, the variable $PSVersionTable contains PowerShell’s de-
tailed version information.

2.7 Scripts and functions

PowerShell provides a scripting language for creating more complex automa-


tions. The scripting language is similar to C#, the .NET programming language,
supporting basic programming language features, designed specifically for a
shell-based environment. (Jones, Hicks & Siddaway 2015, 317.) Scripting allows
combining multiple commands into singular parametrized scripts or functions,
providing features such as conditional execution, looping, variables and arithme-
tic operations. (Jones, Hicks & Siddaway 2015, 61.)

Multiple sequentially executed commands can be placed into a function or script


to make the functionality available by typing a single command. Functions and
scripts can be distributed and executed from script files, which have a .ps1 file
extension. Scripts can be executed by inputting a full file path to the .ps1 file or
with the dot sourcing operator “.” (period). When the dot sourcing operator is
used, the script is run in the current PowerShell session scope, making functions
defined inside the script available until the session is closed. (Microsoft 2017m)
When a script file is executed, it’s code is run as-is in the current session. The
difference between a script and a function is that functions can remain (with dot
sourcing) available in the interactive session as commands. (Microsoft 2017n.) In
figure 7, the TestScript.ps1 script is executed in a PowerShell session, making
the function available to use until the session is closed. Note the dot sourcing
operator used to execute the script.

You might also like