Using scripts for system administration

Submitted by coleen.yan@edd… on Mon, 04/15/2024 - 16:38
Sub Topics

The title “System Administration” covers many different things. According to careers.govt.nz, a systems administrator develops, maintains and administers computer operating systems, database management systems, and security policies and procedures. 

System administration includes many steps from planning what is required, deploying, maintaining, and eventually retiring systems. With the development of virtualisation and cloud computing, what used to take weeks to accomplish has reduced to minutes. Prior to virtualisation if a new system was required someone had to decide what hardware to purchase, then have it shipped, installed, configured, and tested. Now someone can ask for a system, and they can have it in five minutes with cloud technologies. However, all these additional systems need to be monitored and maintained.

This is possible due to automation and in particular, scripting. A system administrator is always going to be looking for a way to avoid repeating the same task. If it must be done manually, there is a significant chance it will not be done. The person may be away or they may be busy with something else. There are all sorts of reasons why it may not be done. If it is automated, the system will do it. In addition, when people do a task, there is always a chance that they will make a mistake. A script will only make a mistake if it is coded incorrectly.

Scripting does not create new programs. Scripting calls upon, and ties existing methods (e.g. troubleshooting methodologies) and tools together.

Troubleshooting methodology

Often, computer technicians spend a large percentage of their time troubleshooting the various software and hardware components used in computers, networks and printers. Before you can even begin to troubleshoot a physical problem with a piece of hardware or diagnose an error thrown up by a software program, you need to understand the basics of troubleshooting and some common best practices.

The most elaborate toolkit and expensive diagnostic software can be useless if you do not have a consistent plan for solving problems. Even experienced technicians can sometimes overlook obvious problems or solutions. Troubleshooting can be extremely challenging, but if you follow basic best practices and conventional troubleshooting procedures, you will often be able to determine the specific cause of a problem, as well as possible solutions to the problem.

A developer troubleshooting a bug that has been raised in a new feature of their organisation's web app

To some extent, being an effective troubleshooter simply involves having a detailed knowledge of how something is supposed to work and of the sort of things that typically go wrong. However, the more complex a system is, the less likely it is that this sort of information will be at hand, so it is important to develop general troubleshooting skills to approach new and unexpected situations confidently. Troubleshooting is a process of problem-solving. It is important to realise that problems have causes, symptoms and consequences.

Consider:

  • A computer system has a fault in the hard disk drive (cause).
  • Because the disk drive is faulty, the computer is displaying a 'blue screen' (symptom).
  • Because of the fault, the user cannot do any work (consequence).

From a business point of view, resolving the consequences of the problem is more important than solving the original cause. For example, the most effective solution might be to provide the user with another workstation, then get the drive replaced.

It is also important to realise that the cause of a specific problem might be the symptom of a larger problem. This is particularly true if the same problem recurs. For example, you might ask why the disk drive is faulty—is it a one-off error or are there problems in the environment, supply chain and so on?

Problem management

Any organisation that has to deal with more than a few problems every week will have a system in place for problem management. The basis of problem management is the identification, prioritisation and ownership of incidents.

The process of problem management is:

  1. A user contacts the help desk, perhaps by phone or email. An operator (or technician) is assigned to the incident and a job ticket is generated.
  2. The user describes the problem to the operator, who may ask clarifying questions. The operator categorises the problem, assesses how urgent it is, and how long it will take to fix.
  3. The operator may take the user through initial troubleshooting steps. If these do not work, the job may be escalated to deskside support or a senior technician.
  4. Troubleshooting continues until the problem is resolved. At that point, the user is contacted to confirm that the problem has been fixed. The job ticket is updated with details of the problem and how it was resolved. The ticket is then considered closed.

At each stage, the problem management system can track the ownership of the problem (who is dealing with it) and its status (what has been done). Each of these stages needs to consider and be guided by corporate policies, procedures and impacts before making changes. An enterprise network is, by definition, highly interconnected. Even small changes can have major, unintended impacts, so it is imperative that you follow established change management procedures and know when to seek authority to make a change.

Developing a troubleshooting mindset

Troubleshooting is not just the application of technical knowledge and expertise; the approach you choose to take is equally important.

Be calm: If you are calm, it instills confidence in the customer and will also prevent you from making rash decisions.

Take breaks: Troubleshooting requires a great deal of concentration. After a long period of working on the same task, the mind can become fatigued and concentration is reduced. Consider taking breaks or leaving the problem for a while to refresh your mind.

Challenge assumptions:  A problem may be reported that is similar to one that you have experienced before, but you should not assume that the problem is identical. Although the symptoms may be similar, the problem and its solution could be completely different. Always treat each problem as a new challenge. Be prepared to try something different. If you have decided what the problem is, but cannot seem to solve it, it may be that you are incorrect. Be prepared to start again from the beginning with an open mind.

Assess costs and impacts:  Remember, you must account for corporate policies and evaluate the impact of a problem. A particular problem might be a stimulating challenge, but if resolving it is not the highest priority in terms of business needs, you need to give up on finding The Answer and use a shortcut to get to a solution. When assessing whether to repair a part, consider the cost of the part and the cost of your time to perform the repair. In many circumstances, replacement is the most effective option.

Know when to give up! You will not always be able to fix the problem yourself. Be prepared to pass the problem on rather than wasting the user's time!

A software developer working with system management tools on their laptop

Being able to view and visualise the diagnostic information of computer systems is of particular importance to systems administrators. Luckily, all modern operating systems make this information relatively easy to access.

Here are a couple of easily accessible tools to visualise diagnostic information in Windows.

Instant Search box and Run Command

The Instant Search box on the Start Menu/Start screen will execute programs and configuration options using simple names. You can open any file or program by pressing the Windows key then typing the path to the file. In the case of registered programs and utilities, you simply need to type the program file name or utility name. Alternatively, you can access the Run dialog box using Windows+R or entering 'run' into the search box.

.MSC extensions and the Run line

There are several management consoles that you can access via the Run line via the .MSC extension. For example:

  • devmgmt.msc opens the Device Manager console
  • diskmgmt.msc opens the Disk Management console
  • compmgmt.msc opens the Computer Management console.

Command line tools

Most configurations of Windows can be done via convenient GUI tools, such as the management consoles and Control Panel. In some circumstances, though, it is necessary to use a command prompt to configure or troubleshoot a system. As you learn the commands, you may also find it quicker to use the command shell for actions (such as file management).

Learning commands is also valuable if you have to write scripts to automate Windows.

Command Prompt

You can run any command from the Run dialog box. However, to input a series of commands or to view output from commands, you need to use the command shell (cmd.exe). To open the prompt, type 'cmd' in the Run dialog box or Instant Search box.

You may need to run the command prompt with elevated privileges in order to execute a command. If a command cannot be run, the error message 'The requested operation requires elevation' is displayed.

You cannot continue within the same window. You need to open a new command prompt as administrator. Right-click the command prompt shortcut and select Run as administrator then confirm the UAC prompt. Alternatively, type 'cmd' in the Instant Search box then press Ctrl + Shift + Enter.

PowerShell is a robust, cross-platform scripting language developed by Microsoft, designed to automate and manage a wide range of tasks across different platforms. It serves as a powerful tool for system administrators, engineers, and IT professionals, offering a command-line interface (CLI) that merges traditional shell capabilities with the flexibility of a sophisticated scripting language.

Core Features


• At its core, PowerShell simplifies administrative tasks by providing a unified, object-oriented

framework for interacting with both Windows and non-Windows systems. It allows users to perform operations such as file and directory management, system configuration, and automation of repetitive tasks, significantly improving productivity and reducing manual effort. One ofthe primary strengths of PowerShell is its object-oriented approach, enabling users to manipulate objects directly within the Windows environment. This method allows for a consistent and intuitive syntax, making it easier to control various system components through objects rather than strings, as seen in other command-line interfaces.
• It enables users to manage files, configure settings, automate workflows, and execute complex operations with ease.


General Principles

Below are some of the core principles and features that make PowerShell unique:
High-Level Abstractions: PowerShell abstracts complex system operations into easy-to-use commands. For example, the cmdlet Get-Process retrieves all active processes on a system, providing
users with a high-level, simplified view of intricate system information.
Pipeline Concept: A powerful feature of PowerShell is its pipeline, which allows the output of one
command to serve as the input for the next. This facilitates chaining multiple operations into a seamless
workflow, enabling complex automation without the need for temporary variables or complex logic.
Object-Centric: Unlike traditional UNIX shells, which rely on string manipulation, PowerShell operates on objects. This object-oriented approach allows users to interact with structured data, passing
entire objects—complete with properties and methods—between commands rather than raw text, simplifying data manipulation and reducing the need for parsing.
Extendable with Cmdlets and Modules: PowerShell’s functionality can be expanded with custom
cmdlets and modules. This extensibility enables users to add new capabilities tailored to specific
environments or tasks, making PowerShell highly adaptable.


Additional Key Features


• PowerShell Remoting: PowerShell supports remote execution, allowing administrators to run scripts
and commands on remote systems securely. This capability is particularly useful in managing large
server farms or cloud environments.
• Background Jobs: PowerShell allows users to run tasks in the background, freeing up the console for
other operations while long-running scripts or processes execute independently.
• Built-in Help System: PowerShell includes an extensive, built-in help system accessible via the
Get-Help cmdlet. This provides detailed documentation on commands, parameters, and usage examples, helping users quickly learn and troubleshoot commands.
• Network File Transfer: PowerShell facilitates file transfers over a network, allowing for seamless
communication between systems and simplifying data handling in distributed environments.
• Aliases: To enhance usability, PowerShell includes aliases for common commands, providing shorthand equivalents that allow users familiar with other shell environments, like UNIX, to get up to speed quickly.
• Cmdlets: PowerShell’s core functionality is built around cmdlets—small, task-specific commands that
perform single actions. These cmdlets can be piped together to perform more complex operations,
offering immense flexibility in script writing and task automation.

Cmdlets and Automation


PowerShell comes with a comprehensive library of cmdlets (short for "command-lets"), which are small,
single-purpose commands that can be combined and piped together to perform more complex tasks. In
addition, users can write custom scripts to automate tasks and tailor system configurations to their specific needs. PowerShell’s ability to pass objects through its pipeline enables the seamless transfer of complex data between commands, making it an ideal tool for automating workflows.


Cross-Platform Versatility


Initially developed for Windows environments, PowerShell has evolved into a cross-platform tool, now supported on Linux and macOS as well. This versatility makes it a valuable asset for managing diverse IT
infrastructures, breaking down traditional operating system silos and allowing users to apply their PowerShell knowledge across multiple platforms.

PowerShell vs Bash


When comparing PowerShell to Bash, the shell commonly used on Linux systems, key differences arise in how the two handle data. Bash works with plain text, passing information between commands as strings, which often requires string manipulation. PowerShell, on the other hand, operates with objects, making it easier to transfer and manipulate complex data structures across commands. Although Bash remains the standard for Linux environments, PowerShell is steadily gaining traction as a cross-platform automation tool.


Some Additional Resources to Explore


PowerShell offers extensive learning resources for users of all levels. Notable resources include:

Microsoft Learn PowerShell
PowerShell Training Paths
The Administrator Crash Course on PowerShell v2

Who Uses PowerShell?


PowerShell is particularly beneficial for professionals in roles such as:
• Systems Administrator
• Network Administrator
• Systems Engineer
• Site Reliability Engineer
• Cloud Architect
Unlike development-focused roles, which often involve languages like Python or C#, PowerShell caters to the needs of IT professionals responsible for managing and automating system tasks.

Why Learn PowerShell?


PowerShell is an essential tool for those seeking to enhance their efficiency and accuracy in managing IT
environments. Here are five compelling reasons to learn PowerShell:
1. Automation: It significantly speeds up task completion.
2. Accuracy: Automation reduces the likelihood of errors.
3. Versatility: PowerShell is compatible with various platforms, including Windows, Linux, and macOS.
4. Community: PowerShell has an active, welcoming community that promotes knowledge sharing and
collaboration.
5. Relevance: PowerShell continues to grow in popularity, making it a critical skill for staying competitive in the job market.

Getting Started with PowerShell


PowerShell can be run either interactively or by executing scripts. To start it interactively, type "PowerShell "into the search bar until the command appears, then press Enter. You can choose between "PowerShell" or "PowerShell ISE" for this purpose, and if you’re using Windows 11, the built-in Terminal also includes a PowerShell interface where you can enter commands directly.
Running commands interactively is useful for testing and experimenting; however, these commands cannot be reused unless they are saved. To make your commands reusable, you need to write them into a script.
Since scripts are simple text files, they can be created using basic text editors like Notepad. However, more specialized tools provide better support for scripting and debugging.
These specialized tools are known as Integrated Development Environments (IDEs). Two popular choices for writing and debugging PowerShell scripts are Visual Studio and Visual Studio Code.

Visual Studio Code (VS Code)

Developed by Microsoft, Visual Studio Code (VS Code) is a free, lightweight, yet powerful source code editor. It is cross-platform and runs on Windows, macOS, Linux, and even Raspberry Pi
OS. VS Code offers several features beneficial for PowerShell development:

  • Debugging support
  • Syntax highlighting
  • Code completion
  • Snippets
  • Code refactoring
  • Built-in Git integration

 

Additionally, VS Code has a PowerShell extension that enhances the experience of writing and managing
PowerShell scripts, making it an excellent choice for both beginners and experienced users.

Activity


Install Visual Studio Code and enter the following code, then run it:
get-vmhost | export-csv $env:userprofile\desktop\vmhost.txt


This will take a few moments to run. Check your desktop for a new file called vmhost.txt.
For details, see this learning resource:

https://learn.microsoft.com/en-us/powershell/scripting/
dev-cross-plat/vscode/using-vscode?view=powershell-7.4&viewFallbackFrom=powershell-7.3

 

Scripting in Windows and Linux OSs


Systems and Network Administrators or Engineers often align themselves with a specific operating system, such as Linux or Windows. This choice may stem from personal preference or the fact that the environment they work in is based on one over the other. So, the type of scripting tools to use also varies, typically, the following examples are common in both Operating Systems:
Linux → Bash → Python
• Windows → PowerShell → C#


However, this is changing with Bash now available on Windows and PowerShell on Linux. The lines between these two environments are beginning to blur, making it worthwhile to explore these two languages.

 

Difference Between CMD vs Powershell vs Bash


Bash is the default shell for most Linux distributions. It combines a scripting language with native Linux
tools to automate and manage Linux devices. PowerShell, meanwhile, is a command-line shell and scripting language developed by Microsoft, originally for Windows but now cross-platform. The following table provides a comparison between Bash and PowerShell.

CMD Powershell Bash
CMD (Command Prompt) is the default command-line interface for Microsoft Windows. It provides basic command-based operations for file management and
system administration.
PowerShell is a task-based
command-line interface designed
for system administrators. Built
on the .NET Framework, it
enhances CMD’s functionality
with advanced automation.
Bash is the default command line and scripting interface for
Unix/Linux systems. It offers advanced scripting capabilities
often used in system administration.
The generated output is simple
text-based characters.
The generated output is simple
text-based characters
PowerShell treats input and output as objects, allowing for more robust data manipulation. Bash, like CMD, processes input and output as text but supports more advanced scripting
and control structures.
CMD uses a straightforward
text-based interface.
PowerShell provides a more interactive, object-oriented
command-line interface (CLI).
Bash has a minimal text-based interface similar to CMD but optimized for Unix/Linux environments.
CMD is included by default in all
Windows operating systems
CMD is included by default in all Windows operating systems. PowerShell is built into Windows
versions starting from 2007. For
earlier versions, it must be installed separately.
Bash is the default  shell in Unix/Linux systems and has
been included since their inception.

Table 1: Comparison of Bash and PowerShell.
Source: AttuneOps.

Objects in PowerShell


In PowerShell, objects are the fundamental elements used to represent and manipulate data. Objects are
instances of .NET classes or PowerShell’s built-in types, encapsulating both data and behavior. They provide a structured and hierarchical way to organize and interact with information.
PowerShell treats everything as an object, including files, directories, processes, and even simple data types like strings and numbers. Objects have properties that store data and methods that define their behavior. This object-oriented nature allows for powerful data manipulation and automation. You can access and modify properties, invoke methods, and chain commands together to perform complex operations on objects. For example, run the following command to explore the properties of files in your current directory:

Get - ChildItem | Format - List

You will notice that each file has several properties, such as:
• Name
• Length (Size)
• CreationTime
• LastWriteTime
• LastAccessTime
• Plus many other details
Each file returned by Get-ChildItem is an object with all these properties. For further details about the
properties associated with files, refer to the official PowerShell documentation.


You can filter and modify objects in PowerShell based on their properties. For instance, try the following
command to list only the .txt files and filter by file size and creation date:

Get - ChildItem | Where - Object { $_ . Name - like "*. txt " - and $_ . Length -gt 100 KB - and $_ . CreationTime -ge ’01/01/2024 ’}

This command returns only .txt files larger than 100KB that were created on or after January 1st, 2023.
Do not worry if you do not understand the commands now - we will dive into them in a
moment.
 

Variables in PowerShell


Variables in PowerShell are placeholders used to store and manipulate data during script execution. They act as containers that can hold various types of information, such as numbers, strings, arrays, or objects. Variables are declared using the $ symbol followed by a name, and their values can be assigned, modified, and retrieved throughout the script.
In this example, Get-Service retrieves all the services running on the system as System.ServiceProcess.ServiceController
objects. The second line declares the $services variable and stores the object returned from Get-Service.
We can confirm this by running the third line, which returns the same object data.
PowerShell supports dynamic typing, meaning variables can hold different data types based on the assigned value. This flexibility allows for automation by facilitating the storage and retrieval of data, enabling scripts to perform operations and make decisions based on the stored values.
For example, the following commands define variables with a simple array and a file path:

$MyVariable = 1 , 2 , 3
$Path = "C :\ Windows \ System32 

Variables can also store the results of commands:

$Processes = Get - Process
$Today = ( Get - Date ). DateTime

When working with file sizes, PowerShell uses readable units such as KB, MB, GB, and TB. This makes specifying sizes like 20MB or 20GB much easier than dealing with bytes directly:

 $MaximumFileSize = 20 MB

Variable Data Types

Here is a demonstration of how PowerShell variables can dynamically change their type:

# Integer operation
$totalSpace = 100GB + 50GB
$totalSpace
$totalSpace | Get-Member
# String operation
$totalSpace = ’100GB + 50GB’
$totalSpace
$totalSpace | Get-Member

In the first case, $totalSpace is an integer because we performed an arithmetic operation. In the second
case, it is a string because the expression was wrapped in quotes.

Strongly Typing Variables

You can enforce variable types to prevent unintended outcomes. For example:

[int]$cpuUsage = ’75’
[int]$memoryUsage = ’2048’
$totalUsage = $cpuUsage + $memoryUsage

Despite the quotes, PowerShell casts the variables as integers, ensuring $totalUsage results in a numerical value.

Converting Variable Types


You can also convert variables between types. For example:

# Convert to string
$usageSummary = $totalUsage.ToString()


Here, we convert $totalUsage, an integer, to a string using the ToString() method.

Practical Example


Let’s apply this knowledge to a real-world task. The script below identifies large files on a system:

$path = Read-Host -Prompt ’Enter the file path to scan for large files:’
$files = Get-ChildItem -Path $path -Recurse
$largeFiles = $files | Where-Object { $_.Length -gt 1GB }
$largeFileCount = $largeFiles.Count
Write-Host "Found $largeFileCount large files in $path"

 

In this script, we prompt the user for a file path, retrieve file information using Get-ChildItem, filter for files larger than 1GB, and display the result.

System Variables


PowerShell also provides access to environment variables, which store system-related information. You can access environment variables using the $env: prefix. For example, the $env:homedrive and $env:homepath variables can be used to access the current user’s home directory:

1. $VMName = " newVM "
2. $VHDPath = " $env : homedrive \ $env : homepath \ $VMName . vhdx "
3.  $VHDPath

# Get a list of environment variables
Get-ChildItem env:

For example, $env:COMPUTERNAME returns the computer’s name, and $env:USERNAME returns the current user.

Activity: Retrieving Environment Variables

Try retrieving the values associated with the following environmental variables:
• COMPUTERNAME
• NUMBER_OF_PROCESSORS
• PROCESSOR_ARCHITECTURE
• SystemDrive
• SystemRoot


You can get a list of all environment variables by running:

 Get - ChildItem env :*

Activity: Assign and Display Environment Variables


Assign the value of the NUMBER_OF_PROCESSORS environment variable to a variable called NumberOfProcessors.
Then, using the Write-Host command, generate a statement that outputs the number of processors in the system:

Solution:


1. $NumberOfProcessors = $env : NUMBER_OF_PROCESSORS
2. Write - Host " The number of processors in my system is $NumberOfProcessors "

Comparison of Data Handling in Bash and PowerShell: Text-Based Strings vs Object-Based Structures

A key difference between Bash and PowerShell is how they handle data. Bash passes everything as strings (plain text), which can be easily moved between commands but often requires additional string manipulation and parsing to extract useful information. While this can be cumbersome, all the tools in Bash’s ecosystem are built around handling simple strings, making it easy to pass data around.


For example, say we want to list files in a directory using Bash, Bash will process everything as text, including the output of commands:

1. # List files in the current directory
2.  ls
3.
4. # Example Output ( Plain Text ):
5.  file1 . txt
6.  file2 . txt
7.  file3 . sh

In Bash, the ls command simply outputs a list of filenames as plain text, with no structured data associated with each file. Bash treats data as raw text, which is useful for basic scripting but less powerful for complex manipulations.
PowerShell, on the other hand, works with objects, allowing complex data to be passed between cmdlets
with minimal effort. This object-oriented approach simplifies the manipulation of structured data but can pose challenges when interacting with non-PowerShell environments since objects are not as easily transferred outside of PowerShell.

PowerShell (Object-Based Handling)

1. # List files in the current directory
2.  Get - ChildItem
3.

4. # Example Output (Object - Based ):
5.  Directory : C :\ Users \ Documents
6.
7. Mode LastWriteTime Length Name
8. ---- ------------- ------ ----
9. -a ---- 10/21/2024 10:32 AM 50 file1 . txt
10. -a ---- 10/21/2024 10:35 AM 150 file2 . txt
11. -a ---- 10/21/2024 11:00 AM 200 file3 . sh

In PowerShell, Get-ChildItem outputs objects, where each file has properties like Name, LastWriteTime, and Length, allowing structured interaction.

Bash and Python vs PowerShell


While a Bash/Python combination offers similar capabilities to PowerShell, PowerShell can’t fully replace the advanced features that Python provides for scripting and automation. Bash remains the de facto standard for Linux users, while PowerShell is the go-to for Windows environments. Each continues to evolve, and both are likely to become better suited for managing cross-platform tasks.
Ultimately, the choice between Bash and PowerShell may come down to personal preference: whether you prefer working with strings or objects. For those who favor structured data and object manipulation, PowerShell stands out.


Where Can You Use PowerShell?


In today’s IT environments, it’s increasingly common to encounter hybrid or cross-platform systems. Learning PowerShell is highly beneficial since it supports nearly every major platform, including Windows, Linux, and macOS. This versatility makes PowerShell a powerful tool for administrators who need to manage diverse technology stacks.


Essential PowerShell Concepts and Commands
Before diving into key commands, it’s essential to understand some basics about PowerShell. PowerShell
is a command-line shell and scripting language designed to help administrators and power users automate tasks efficiently. Its primary design goals include ease of use, discoverability, and consistency, enabling even newcomers to quickly pick it up and start using it effectively.
Note: To follow along, you’ll need access to PowerShell.
• Windows Users: PowerShell comes pre-installed. Simply search for PowerShell in your Start menu
and launch it.
• Linux Users: You can install PowerShell by following the official guide: Install PowerShell on Linux - PowerShell | Microsoft Learn
• macOS Users: PowerShell is also available on macOS. Install it by following the instructions: Installing PowerShell on macOS - PowerShell | Microsoft Learn

Verb-Noun Structure


In PowerShell, cmdlets follow a Verb-Noun structure, which improves accessibility and reduces the need for command memorization. The cmdlet format is Verb-Noun. For example:
Get-TimeZone


This cmdlet retrieves the current time zone. Based on the Verb-Noun structure, you can infer that there
might also be a Set-TimeZone cmdlet for adjusting the time zone settings. This consistent structure makes cmdlets easier to guess and discover.


For example, we can have Cmdlets such as :
Get-Process: Retrieves system processes.
Start-Process: Initiates a new process.
Stop-Process: Terminates a process.
For a complete list of approved PowerShell verbs, refer to the official documentation

Cmdlets
PowerShell commands are known as cmdlets. Each cmdlet performs a specific action and typically returns an object to the pipeline. Let’s run your first cmdlet:
Get-TimeZone
Congratulations! You just executed your first cmdlet. The Get-TimeZone cmdlet returns an object containing data about your device’s current time zone configuration.

Using Get-Command


The Get-Command cmdlet helps you discover available commands on your system. To get a list of all available cmdlets, use:
Get-Command *


The asterisk (*) is a wildcard representing “everything.” This command retrieves a comprehensive list of all available cmdlets. To narrow down your search, specify part of the cmdlet name. For instance, to focus on processes, try:

Get-Command *process*


This command lists cmdlets related to processes:
Get-Process
Start-Process
Stop-Process
Wait-Process
ConvertTo-ProcessMitigationPolicy
Basically, this command retrieves commands that have "process" in them.

What if Get-Command Doesn’t Help?


Sometimes, the cmdlet you’re looking for may not be obvious. For instance, you might think there’s a cmdlet called Get-File, but using:
Get-Command *file* reveals no results. In such cases, an internet search can be beneficial. A quick search will show that Get-Item is the cmdlet you need to retrieve file information. When in doubt, leverage external resources like online forums and documentation.

Get-Help Cmdlet


If you’re unsure what a cmdlet does, use Get-Help to obtain detailed information, usage examples, and
syntax. Before using it, it’s a good idea to update your help files with:
Update-Help
(Ensure you run PowerShell CLI or ISE as administrator. as Update-Help usually requires administrative privileges to update the help files). This command downloads the latest help documentation. Now, you can use Get-Help to learn about any cmdlet.

Using Get-Help to Explore PowerShell Commands


The Get-Help cmdlet is a powerful tool in PowerShell that provides help documentation for various cmdlets, concepts, and even general topics. The following commands demonstrate how to leverage Get-Help for different purposes.
Below is an example demonstrating how to use the Get-Help cmdlet to retrieve information about the
Write-Host cmdlet. Note that you replicate these examples for any other cmdlet.

Get-Help Write-Host: This command retrieves general help information about the Write-Host cmdlet.
It includes:
• A description of what the cmdlet does.
• Syntax information, including the parameters it accepts.
• Detailed explanations of how the cmdlet works.
When you run this command, PowerShell displays an overview of the cmdlet, helping you understand its
basic usage.

  • NAME

Write-Host

 

  • SYNOPSIS

Displays text or other output to the host.

 

  • SYNTAX

Write-Host [[-Object] <Object>] [-NoNewline] [-Separator <Object>]
[-ForegroundColor <ConsoleColor>] [-BackgroundColor <ConsoleColor>]
[<CommonParameters>]

 

  • DESCRIPTION

The Write-Host cmdlet writes customized output to the console. You can specify colors, no new line, and text formatting options.

Help for About Topics


To learn about conceptual topics in PowerShell, use:
Get-Help About_*
This will return a list of all about topics, which provide details on key PowerShell concepts and features.
Listing Get-* Cmdlets
To display a list of all cmdlets that start with Get-, use the following command and format the output as a table:
Get-Help Get-* | Format-Table Name
This command lists all Get-* cmdlets, which are typically used for retrieving information from various
sources.

Get Online Help


If you want to access the online documentation for a cmdlet, use:
Get-Help Get-Help -Online
This opens the most up-to-date online documentation for the Get-Help cmdlet in your browser.
Get-Help Write-Host -Online:
This command opens the official PowerShell documentation for the Write-Host cmdlet in your default web browser.

Get Examples for a Specific Cmdlet


To quickly understand how a cmdlet works, you can retrieve its examples using:
Get-Help Get-EventLog -Examples
This displays practical examples for using the Get-EventLog cmdlet.


Get-Help Write-Host -Example
This command focuses specifically on examples of how to use the Write-Host cmdlet. It displays real-world examples, showing different ways the cmdlet can be used with various parameters.

  • NAME

Write-Host

  • EXAMPLES

Example 1: Writing a simple string


Write-Host "Hello, World!" 

Example 2: Display text in different colours

Write-Host "This is red text" -ForegroundColor Red


Example 3: Write output without a new line
Write-Host "Output without newline" -NoNewline


These examples demonstrate practical usage, making it easier for users to understand how the cmdlet worksin different contexts.


Get-Help Stop-Process -Examples
This command shows practical examples of how to use Stop-Process

Get Detailed Help


To view more in-depth information about a cmdlet, including parameter descriptions, use:
Get-Help Get-EventLog -Detailed


Get Full Help Information


For the most comprehensive help documentation, including all available parameters and examples, use:
Get-Help Get-EventLog -Full

Understanding Help Output


When using Get-Help, the following options help you interpret and work with the help content effectively:
Get-Help (without parameters): This provides a brief overview of the command, including its syntax
and description.
Examples:
– Use this to gather a quick understanding of a cmdlet based on practical examples.
– This option is particularly useful for quickly getting started with a new command.
Full:
– Retrieves the complete documentation for a cmdlet, including all parameter details and examples.
– This option is useful when you need to understand how to use the command comprehensively, even
without prior knowledge.
Detailed:
– Provides detailed help, similar to -Full, but focuses on parameter descriptions and usage specifications.
– Contains examples, making it a more specific resource than -Full.


Understanding Objects Using Get-Member


Unlike Bash, which works with plain text, PowerShell operates on objects. The Get-Member cmdlet allows you to explore these objects, revealing their properties and methods.

 

  • Example:

Get-Date | Get-Member
This command shows that the object returned by Get-Date possesses various properties such as Day, Month, Year, etc.
To ensure that all properties are displayed, you can use:
Get-Date | Format-List
This command displays all properties of the Get-Date object in a list format.

 

  • Simpler Example:

Get-Random | Get-Member
This command returns a simple System.Int32 object, as Get-Random generates a random integer.
With these basics—Get-Command, Get-Help, Get-Member, and understanding the Verb-Noun structure—you’re well on your way to mastering PowerShell. These core tools will help you explore and understand the cmdlets and objects available, empowering you to automate tasks and manage systems effectively.

 

 


 


 


 

 

 

 

 




 

 

 

 

Variables in PowerShell are placeholders used to store and manipulate data during script execution. They act as containers that can hold various types of information, such as numbers, strings, arrays, or objects. Variables are declared using the $ symbol followed by a name, and their values can be assigned, modified, and retrieved throughout the script. PowerShell supports dynamic typing, allowing variables to hold different data types based on assignment. They enable flexibility and automation by facilitating the storage and retrieval of data, allowing scripts to perform operations and make decisions based on the stored values.

$MyVariable = 1, 2, 3
$Path = "C:\Windows\System32"

Variables can be used to store the results of commands.

$Processes = Get-Process $Today = (Get-Date).DateTime

When working with bytes PowerShell uses KB, MB, GB, and TB to make computer sizes more readable. 20MB or 20GB or 20KB is much easier when the system is expecting bytes.

$MaximumFileSize = 20MB

There are system variables such as $env:homedrive and $env:homepath that can be used. The $env: tells PowerShell to look for the “environment variable”. In this case, the default home drive and home path for the user will create a way to access the current user’s home folder.

$VMName = "newVM"
$VHDPath = "$env:homedrive + $env:homepath + "\" + $VMName + ".vhdx"
$VHDPath

Activity

Get the value associated with each of the following environmental variables:

  • COMPUTERNAME
  • NUMBER_OF_PROCESSORS
  • PROCESSOR_ARCHITECTURE
  • SystemDrive
  • SystemRoot

To get a list of all the environmental variable can use the command:

get-childitem env:*

Activity

Assign the value of the environment variable Number_of_Processors to a variable called NumberOfProcessors. Using the Write-Host command have it generate the complete statement “The number of processors in my system is (number of processors)".

$NumberOfProcessors = $env:Number_Of_Processors
write-host “The number of processors in my system is $NumberOfProcessors”)
  

Operators in PowerShell are symbols or keywords used to perform various operations on data. They enable computations, comparisons, and logical evaluations within scripts. PowerShell supports a wide range of operators, including arithmetic operators, comparison operators, assignment operators, and logical operators. These operators allow for mathematical calculations, comparisons of values, logical evaluations, and control flow in PowerShell scripts. They are fundamental tools that help manipulate data, make decisions, and automate tasks based on certain conditions or criteria.

Arithmetic operators in PowerShell are symbols used to perform mathematical computations. The + operator adds numbers, the - operator subtracts them, the * operator multiplies them, and the / operator divides them. The % operator calculates the remainder of a division. For example, $a = 5 + 3 sets the value of $a to 8, and $b = 10 / 2 sets the value of $b to 5. These operators allow for numerical calculations and are commonly used in PowerShell scripts for mathematical operations.

Comparison operators in PowerShell are used to compare values and determine their relationship. Comparison operators in PowerShell are used to compare values and determine their relationship.

  • -gt (greater than)
  • -lt (less than)
  • -ge (greater than or equal to)
  • -eq (equals)
  • -le (less than or equal to)
  • -ne (not equal to)

These can be used to compare both numerical values and strings. For example:

  • “box” -gt “alpha” is true because ‘b’ comes after ‘a’.
  • 10 -gt 9 is true because comparing the values but
  • ‘10’ -gt ‘9’ is false because “9” as a string is greater than “1” as a string.

When working with strings there are another couple of common operators:

  • -like – can use a wildcard so that -like ‘*shell*’ is anything with shell in it
  • -notlike – is anything that does not match the “like” criteria

Use the -like comparison when you want to find all the instances that have a particular string. For example, if you want to find all the files that have the filetype “.txt”. The command “get-childitem” will retrieve all the files in the current folder and then the following command will filter the files to find all the .txt files.

Get-childitem | where {$_.Name -like "*.txt"}

In most cases, using the verb “get” in PowerShell will return objects. Get-VM will return a series of objects about the VMs on the system. Any of the properties can be used to filter the objects returned by using a “where” statement. The $_ is a shorthand for “the objects returned by the previous cmdlet”. Try modifying the above command to select a different extension such as “.doc”. For details about operators see: about Operators - PowerShell | Microsoft Learn.

In PowerShell, objects are fundamental elements used to represent and manipulate data. Objects are instances of .NET classes or PowerShell's built-in types, encapsulating both data and behaviour. They provide a structured and hierarchical way to organize and interact with information. PowerShell treats everything as an object, including files, directories, processes, and even simple data types like strings and numbers. Objects have properties that store data and methods that define their behaviour. PowerShell's object-oriented nature allows for powerful data manipulation and automation. It enables accessing and modifying properties, invoking methods, and chaining commands together to perform complex operations on objects. Try running the following command:

get-childitem | format-list

You will see that each file has a lot of information associated with it including:

  • Name
  • Length
  • CreationTime
  • LastWriteTime
  • LastAccessTime
  • plus many other items.

Each file returned is an object with all the properties. This site provides information about the properties associated with files: PowerShell | Microsoft Learn

Try modifying the following command to filter for Size of file ($_.Length) and Files created after or before 1/1/2023:

Get-childitem | where {$_.Name -like "*.txt"}

Using flow control, scripts can change how they run in response to specific conditions. Loops, switches, and if statements allow for data filtering, repeated operations, and selective code execution. Script flexibility, automation, and the capacity to effectively handle a variety of circumstances are all improved with flow control. Conditional We previously looked at using “Where” to identify large files.

$LargeFiles = Get-childitem | Where {$_.Length -gt $MaximumFileSize}

This works well as a filter for results, it doesn’t allow for different actions to be performed based on the condition. To do this we can use an “IF” statement.

if $f.Length -gt $MaximumFileSize {
    write-host “$f.Name is over $MaximumFileSize” 
} else {
    write-host “$f.Name is no larger than $MaximumFileSize”
}

An IF statement in PowerShell evaluates a specified condition and executes a block of code based on the result. Here's how it works:

  1. The condition is evaluated. It can be a comparison, a logical expression, or any valid condition.
  2. If the condition evaluates to true, the code block within the IFstatement is executed.
  3. If the condition is false, the code block within the else statement (if provided) is executed instead.
  4. After executing the appropriate code block, the script continues executing the subsequent statements after the IF statement.

The IF statement allows for the selective execution of code based on conditions, providing the ability to control the flow and behaviour of the script based on specific criteria or situations.

Point to think about: Why was the output for the “else” statement “is no larger than”? What would happen if the file size is the same as $MaximumFileSize? A fuller description of the power of the IF statement together with some system administration applications can be found at: Back to Basics: Conditional Logic with PowerShell If-Else.

More technical details of the IF statement can be found at: about If - PowerShell | Microsoft Learn.

Nested IF statements are hard to read. It is easy to lose track of where the logic is, so if you are wanting to do an action based on whether it is a, b, or c a switch statement is better and easier to read. For example a menu. The user can enter “1”, “2”, “3” or “q”. Any other entry is invalid.

$ans = read-host "Please enter selection (1,2,3, or q)"
       switch ($ans) {
		1 {"1 selected" }
		2 {"2 selected" }
		3 {"3 selected" }
		q {"exit"}
		Default {"Please enter 1, 2, 3, or q"}
	}

For more details see: Back to Basics: Understanding the PowerShell Switch Statement.

Loops

The following statement collects a list of large files, where the file size is over $MaximumFileSize.

$LargeFiles = Get-childitem | Where {$_.Length -gt $MaximumFileSize}

Using a “Foreach” command, each file that is collected can be processed.

Foreach ($L in $LargeFiles)
		{
		$L.Name + “ is “ +_$L.Length		
                }

The other loop option in PowerShell is “DO”. All DO loops get executed at least once and are then rerun depending on whether the WHILE is true or the UNTIL is false.

$i = 1
DO { 
  Write-host ‘$i is ‘ $i
  $i = $i + 1
  } while ($i -le 10)

$I = 1
DO {
  Write-host ‘$i is ‘ $i
  $i = $i + 1
  } UNTIL ($i -gt 10)

It is important to make sure that the UNTIL will eventually become true or the WHILE will become false otherwise will have an infinite loop. There are more options available. The most widely used ones are described at Flow control - PowerShell | Microsoft Learn.

Navigating and viewing files and directories

Windows PowerShell uses the noun 'Location' to refer to the working directory and implements a family of cmdlets (lightweight commands) to examine and manipulate your location.

To determine the path of your current directory location, enter the command:

Get-Location

# Output
C:\Documents and Settings\PowerUser

The Get-Location command is used with the Set-Location command. The Set-Location command allows you to specify your current directory location.

Set-Location -Path C:\Windows

After you enter the command, you will notice that you do not receive any direct feedback about the effect of the command. Most Windows PowerShell commands that perform an action produce little or no output because the output is not always useful. To verify that a successful directory change has occurred when you enter the Set-Location command, include the -PassThru parameter when you enter the command:

Set-Location -Path C:\Windows -PassThru

# Output
C:\WINDOWS

The -PassThru parameter can be used with many Set- commands in Windows PowerShell to return information about the result in cases in which there is no default output.

You can specify paths relative to your current location in the same way as you would in most UNIX and Windows command shells. In standard notation for relative paths, a period (.) represents your current folder, and a doubled period (..) represents the parent directory of your current location.

For example, if you are in the C:\Windows folder, a period (.) represents C:\Windows and double periods (..) represents C:. You can change from your current location to the root of the C: drive by typing:

Set-Location -Path .. -PassThru

# Output
C:\

The same technique works on Windows PowerShell drives that are not file system drives, such as HKLM:. You can set your location to the HKLM\\Software key in the registry by typing:

Set-Location -Path HKLM:\SOFTWARE -PassThru

# Output
HKLM:\SOFTWARE

You can then change the directory location to the parent directory, which is the root of the Windows PowerShell HKLM: drive, by using a relative path:

Set-Location -Path .. -PassThru

# Output
HKLM:\

You can type Set-Location or use any of the built-in Windows PowerShell aliases for Set-Location (cd, chdir, sl). For example:

cd -Path C:\Windows
chdir -Path .. -PassThru
sl -Path HKLM:\SOFTWARE -PassThru

When changing locations, it is helpful to keep track of where you have been and to be able to return to your previous location. The Push-Location cmdlet in Windows PowerShell creates an ordered history (a 'stack') of directory paths where you have been, and you can step back through the history of directory paths by using the complementary Pop-Location cmdlet.

For example, Windows PowerShell typically starts in the user's home directory.

Get-Location

# Output
C:\Documents and Settings\PowerUser

The word stack has a special meaning in many programming settings, including .NET Framework. Like a physical stack of items, the last item you put onto the stack is the first item that you can pull off the stack. Adding an item to a stack is colloquially known as 'pushing' the item onto the stack. Pulling an item off the stack is colloquially known as 'popping' the item off the stack.

To push the current location onto the stack, and then move to the Local Settings folder, type:

Push-Location -Path "Local Settings"

You can then push the Local Settings location onto the stack and move to the Temp folder by typing:

Push-Location -Path Temp

You can verify that you changed directories by entering the Get-Location command:

Get-Location

# Output
C:\Documents and Settings\PowerUser\Local Settings\Temp

You can then pop back into the most recently visited directory by entering the Pop-Location command, and verify the change by entering the Get-Location command:

Pop-Location
Get-Location

# Output
C:\Documents and Settings\me\Local Settings

Just as with the Set-Location cmdlet, you can include the PassThru parameter when you enter the Pop-Location cmdlet to display the directory that you entered:

Pop-Location -PassThru

# Output
C:\Documents and Settings\PowerUser

You can also use the Location cmdlets with network paths. If you have a server named FS01 with a share named Public, you can change your location by typing:

Set-Location \\FS01\Public

or

Push-Location \\FS01\Public

You can use the Push-Location and Set-Location commands to change the location to any available drive. For example, if you have a local CD-ROM drive with drive letter D that contains a data CD, you can change the location to the CD drive by entering the Set-Location D: command.

If the drive is empty, you will get the following error message:

Set-Location D:

# Output
Set-Location : Cannot find path 'D:\' because it does not exist.

When you are using a command-line interface, it is not convenient to use File Explorer to examine the available physical drives. Further, File Explorer would not show you all of the Windows PowerShell drives. (Windows PowerShell provides a set of commands for manipulating Windows PowerShell drives, and we will talk about these after you have watched the video below.)

Working with files and directories

Navigating through Windows PowerShell drives and manipulating the items on them is similar to manipulating files and folders on Windows physical disk drives. This section discusses how to deal with specific file and folder manipulation tasks using PowerShell.

You can get all items directly within a folder by using Get-ChildItem. Add the optional Force parameter to display hidden or system items. For example, this command displays the direct contents of Windows PowerShell Drive C (which is the same as the Windows physical drive C):

Get-ChildItem -Path C:\ -Force

The command lists only the directly contained items, much like using Cmd.exe's DIR command or ls in a UNIX shell. In order to show contained items, you need to specify the -Recurse parameter as well. (This can take an extremely long time to complete.) To list everything on the C drive:

Get-ChildItem -Path C:\ -Force -Recurse

Get-ChildItem can filter items with its Path, Filter, Include and Exclude parameters, but those are typically based only on name. You can perform complex filtering based on other properties of items by using Where-Object.

The following command finds all executables within the Program Files folder that were last modified after 1 October 2005 and which are neither smaller than 1 megabyte nor larger than 10 megabytes:

Get-ChildItem -Path $env:ProgramFiles -Recurse -Include *.exe | Where-Object -FilterScript {($_.LastWriteTime -gt '2005-10-01') -and ($_.Length -ge 1mb) -and ($_.Length -le 10mb)}

Copying files and folders

Copying is done with Copy-Item. The following command backs up C:\boot.ini to C:\boot.bak:

Copy-Item -Path C:\boot.ini -Destination C:\boot.bak

If the destination file already exists, the copy attempt fails. To overwrite a pre-existing destination, use the Force parameter:

Copy-Item -Path C:\boot.ini -Destination C:\boot.bak -Force

This command works even when the destination is 'read-only'.

Folder copying works the same way. This command copies the folder C:\temp\test1 to the new folder C:\temp\DeleteMe recursively (repeatedly):

Copy-Item C:\temp\test1 -Recurse C:\temp\DeleteMe

You can also copy a selection of items. The following command copies all .txt files contained anywhere in C:\data to C:\temp\text:

Copy-Item -Filter *.txt -Path c:\data -Recurse -Destination C:\temp\text

You can still use other tools to perform file system copies. XCOPY, ROBOCOPY and COM objects (such as the Scripting.FileSystemObject) all work in Windows PowerShell. For example, you can use the Windows Script Host Scripting.FileSystem COM class to backup.

C:\boot.ini to C:\boot.bak:
(New-Object -ComObject Scripting.FileSystemObject).CopyFile('C:\boot.ini', 'C:\boot.bak')

Creating files and folders

Creating new items works the same on all Windows PowerShell providers. If a Windows PowerShell provider has more than one type of item (e.g. the FileSystem Windows PowerShell provider distinguishes between directories and files) you need to specify the item type.

This command creates a new folder C:\temp\New Folder:

New-Item -Path 'C:\temp\New Folder' -ItemType Directory

This command creates a new empty file C:\temp\New Folder\file.txt

New-Item -Path 'C:\temp\New Folder\file.txt' -ItemType File

Important: When using the Force switch with the New-Item command to create a folder, and the folder already exists, it won't overwrite or replace the folder. It will simply return the existing folder object. However, if you use New-Item -Force on a file that already exists, the file will be completely overwritten.

Removing all files and folders within a folder

You can remove contained items using Remove-Item, but you will be prompted to confirm the removal if the item contains anything else. For example, if you attempt to delete the folder C:\temp\DeleteMe that contains other items, Windows PowerShell prompts you for confirmation before deleting the folder:

Remove-Item -Path C:\temp\DeleteMe
Confirm
The item at C:\temp\DeleteMe has children and the Recurse parameter was not
specified. If you continue, all children will be removed with the item. Are you
sure you want to continue?
[Y] Yes  [A] Yes to All  [N] No  [L] No to All  [S] Suspend  [?] Help
(default is "Y"):

If you do not want to be prompted for each contained item, specify the Recurse parameter:

Remove-Item -Path C:\temp\DeleteMe -Recurse

Mapping a local folder as a drive

You can also map a local folder, using the New-PSDrive command. The following command creates a local drive P: rooted in the local Program Files directory, visible only from the PowerShell session:

New-PSDrive -Name P -Root $env:ProgramFiles -PSProvider FileSystem

Just as with network drives, drives mapped within Windows PowerShell are immediately visible to the Windows PowerShell shell. In order to create a mapped drive visible from File Explorer, the parameter -Persist is needed. However, only remote paths can be used with Persist.

Reading a text file into an array

One of the more common storage formats for text data is in a file with separate lines treated as distinct data elements. The Get-Content cmdlet can be used to read an entire file in one step, as shown here:

PS> Get-Content -Path C:\boot.ini
[boot loader]
timeout=5
default=multi(0)disk(0)rdisk(0)partition(1)\WINDOWS
[operating systems]
multi(0)disk(0)rdisk(0)partition(1)\WINDOWS="Microsoft Windows XP Professional"
/noexecute=AlwaysOff /fastdetect
multi(0)disk(0)rdisk(0)partition(1)\WINDOWS=" Microsoft Windows XP Professional
with Data Execution Prevention" /noexecute=optin /fastdetect

Get-Content already treats the data read from the file as an array, with 1 element per line of file content. You can confirm this by checking the Length of the returned content:

PS> (Get-Content -Path C:\boot.ini).Length

This command is most useful for getting lists of information into Windows PowerShell directly. For example, you might store a list of computer names or IP addresses in a file C:\temp\domainMembers.txt, with 1 name on each line of the file. You can use Get-Content to retrieve the file contents and put them in the variable $Computers:

$Computers = Get-Content -Path C:\temp\DomainMembers.txt

$Computers is now an array containing a computer name in each element.

Check your knowledge

Answer these three questions about programming and scripting. 

PowerShell script activities

Instructions

Watch the following video and trial a few of the scripts to test out some basic PowerShell scripts.

Share a screen capture of your attempts in the forum.

The following pages provide important tools for working in system administration. Examine the topics and scripts under each at this Microsoft Learn page. Pay particular attention to topics such as:

  • The items under “Working with objects”
  • Getting information about computer
  • Working with VMs.

Activity

Use the new-vm cmdlet to create a new virtual machine. You will need to:

  • Give the VM a name
  • Specify the amount of RAM
  • Create a new virtual hard disk (vhdx)
  • Add a DVD drive so can install Windows.

Once you have created the VM configure by:

  • Renaming to VMWS01
  • Setting the IP configuration to:
    • IPv4 Address: 192.168.0.50
    • Subnet mask: 255.255.255.0
    • Default gateway: 192.168.0.250
    • DNS server: 9.9.9.9
  • Set the time zone to New Zealand
  • Set the region and language to New Zealand

After configuring the VM, practice collecting information for that machine such as:

  • CPU
  • RAM
  • running processes
  • Windows details
  • Information about the volumes 

While can use commands like read-host and write-host to put information on the screen and get it from the screen these are not very user friendly.

PowerShell's integration with .NET enables the use of forms in PowerShell scripts. The .NET Framework includes the System.Windows.Forms namespace, which provides classes for creating graphical user interfaces (GUI) with forms, controls, and events. This allows PowerShell scripts to create and customize GUI applications, build interactive forms, handle user input, and respond to events. The integration with the .NET Framework empowers PowerShell to leverage the extensive GUI capabilities of Windows Forms, enhancing the user experience and enabling the development of rich, interactive applications.

Creating a form

Take a look at the following code:


Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
$form = New-Object System.Windows.Forms.Form
$form.Size = New-Object System.Drawing.Size(300,200)
$form.text = 'Data Entry Form'
$form.startPosition = 'CentreScreen'
$form.ShowDialog()

Executing this code should display a form in the middle of the screen. At this stage, the only way to close the window, is to click the “close” in the top right corner. This will result in the word “Cancel” appearing in the output.

Starting at the top, Add-Type -AssemblyName System.Windows.Forms adds a reference to the System.Windows.Forms assembly, which provides functionality for creating Windows-based applications with forms and controls.

Add-Type -AssemblyName System.Drawing adds a reference to the System.Drawing assembly, which provides classes for graphics and image manipulation. By adding these assemblies, you can utilize the classes and functionality they provide within your PowerShell script, allowing you to create GUI applications or perform graphic-related tasks. The lines of code that follow create and configure a Windows Form object using the .NET Framework's System.Windows.Forms namespace:

  1. $form = New-Object System.Windows.Forms.Form creates a new instance of a Windows Form object and assigns it to the variable $form.
  2. $form.Size = New-Object System.Drawing.Size(300,200) sets the size of the form to 300 pixels wide and 200 pixels high using the System.Drawing.Size class.
  3. $form.text = 'Data Entry Form' sets the text property of the form to 'Data Entry Form', which will be displayed in the title bar of the form.
  4. $form.startPosition = 'CentreScreen' sets the position of the form to the centre of the screen when it is displayed.

$form.ShowDialog() displays the form as a modal dialogue box. When this line is executed, the form created earlier using the $form variable will be shown on the screen, and it will remain in focus until it is closed by the user. The script execution will pause at this point, waiting for the user to interact with the form. This behaviour allows for capturing user input or performing actions based on user interaction before the script continues executing further statements after the ShowDialog() method call.

Try changing the form.size values to see what effect it has.

Adding objects to the form

At this stage we have created a blank canvas, now we need to add some item. The first item to be added will be an “Ok” button. The following lines of code create an "OK" button, define its properties and behavior, and add it to the form's controls collection, allowing the user to interact with the button within the form. Add the lines before $form.ShowDialog() in the script.

$okButton = New-Object System.Windows.Forms.Button
$okButton.Location = New-Object System.Drawing.Point(75,120)
$okButton.Size = New-Object System.Drawing.Size(75,23)
$okButton.Text = 'OK'
$okButton.DialogResult = [System.Windows.Forms.DialogResult]::OK
$form.AcceptButton = $okButton
$form.Controls.Add($okButton)

  1. $okButton = New-Object System.Windows.Forms.Button creates a new instance of a button control and assigns it to the variable $okButton.
  2. $okButton.Location = New-Object System.Drawing.Point(75,120) sets the position of the button on the form using the System.Drawing.Point class.
  3. $okButton.Size = New-Object System.Drawing.Size(75,23) sets the size of the button using the System.Drawing.Size class.
  4. $okButton.Text = 'OK' sets the text displayed on the button to 'OK'.
  5. $okButton.DialogResult = [System.Windows.Forms.DialogResult]::OK sets the dialog result of the button to OK, which indicates that when the button is clicked, the dialog result of the form will be set to OK.
  6. $form.AcceptButton = $okButton sets the form's accept button, which determines which button is triggered when the user presses the Enter key.
  7. $form.Controls.Add($okButton) adds the button control to the form's collection of controls, making it visible on the form.

Try changing the values for size and location to see what effect they have.

If the user changes their mind, we need to provide an opportunity to cancel. This is done by adding a second button. Compare the code below to the code for the OK button to see how it provides different information to the script.

$cancelButton = New-Object System.Windows.Forms.Button
$cancelButton.Location = New-Object System.Drawing.Point(150,120)
$cancelButton.Size = New-Object System.Drawing.Size(75,23)
$cancelButton.Text = 'Cancel'
$cancelButton.DialogResult = [System.Windows.Forms.DialogResult]::Cancel
$form.CancelButton = $cancelButton
$form.Controls.Add($cancelButton)

The form is now functional with an OK and cancel button but does not have any way of allowing user input. The following lines of code create a TextBox control, define its position and size, and adds it to the form's controls collection. This allows the user to input text within the TextBox control on the form.

$textBox = New-Object System.Windows.Forms.TextBox
$textBox.Location = New-Object System.Drawing.Point(10,40)
$textBox.Size = New-Object System.Drawing.Size(260,20)
$form.Controls.Add($textBox)
  1. $textBox = New-Object System.Windows.Forms.TextBox creates a new instance of a TextBox control and assigns it to the variable $textBox.
  2. $textBox.Location = New-Object System.Drawing.Point(10,40) sets the position of the TextBox control on the form using the System.Drawing.Point class.
  3. $textBox.Size = New-Object System.Drawing.Size(260,20) sets the size of the TextBox control using the System.Drawing.Size class.
  4. $form.Controls.Add($textBox) adds the TextBox control to the form's collection of controls, making it visible on the form.

You can use label text on your window to describe the information you want users to provide. The following lines of code create a Label control, define its position, size, and text, and add it to the form's controls collection. The Label control displays a descriptive text message on the form, providing guidance or information to the user. Add these lines above the textbox code.

$label = New-Object System.Windows.Forms.Label
$label.Location = New-Object System.Drawing.Point(10,20)
$label.Size = New-Object System.Drawing.Size(280,20)
$label.Text = 'Please enter the information in the space below:'
$form.Controls.Add($label)
  1. $label = New-Object System.Windows.Forms.Label creates a new instance of a Label control and assigns it to the variable $label.
  2. $label.Location = New-Object System.Drawing.Point(10,20) sets the position of the Label control on the form using the System.Drawing.Point class.
  3. $label.Size = New-Object System.Drawing.Size(280,20) sets the size of the Label control using the System.Drawing.Size class.
  4. $label.Text = 'Please enter the information in the space below:' sets the text displayed on the Label control to 'Please enter the information in the space below:'.
  5. $form.Controls.Add($label) adds the Label control to the form's collection of controls, making it visible on the form.

To finish the form, add the following lines after the textbox code.

$form.Topmost = $true

$form.Add_Shown({$textBox.Select()})
$result = $form.ShowDialog()

if ($result -eq [System.Windows.Forms.DialogResult]::OK)
{
    $x = $textBox.Text
    $x
}
  1. $form.Topmost = $true sets the Topmost property of the form to true, ensuring that the form is displayed as the topmost window on the screen.
  2. $form.Add_Shown({$textBox.Select()}) attaches an event handler to the form's Shown event. When the form is shown, the event handler selects the $textBox control, placing the cursor in the text box for user input.
  3. $result = $form.ShowDialog() displays the form as a modal dialog box, waits for the user to interact with the form, and assigns the dialog result (OK, Cancel, etc.) to the $result variable.
  4. The if ($result -eq [System.Windows.Forms.DialogResult]::OK) statement checks if the user clicked the OK button or pressed Enter (assuming OK is the form's AcceptButton). If true, the code block within the if statement is executed.
  5. $x = $textBox.Text assigns the text entered in the $textBox control to the variable $x.
  6. $x outputs the value of $x, displaying the entered text or performing further operations with it.

Your completed script should look like this.

Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing

$form = New-Object System.Windows.Forms.Form
$form.Text = 'Data Entry Form'
$form.Size = New-Object System.Drawing.Size(300,200)
$form.StartPosition = 'CenterScreen'

$okButton = New-Object System.Windows.Forms.Button
$okButton.Location = New-Object System.Drawing.Point(75,120)
$okButton.Size = New-Object System.Drawing.Size(75,23)
$okButton.Text = 'OK'
$okButton.DialogResult = [System.Windows.Forms.DialogResult]::OK
$form.AcceptButton = $okButton
$form.Controls.Add($okButton)

$cancelButton = New-Object System.Windows.Forms.Button
$cancelButton.Location = New-Object System.Drawing.Point(150,120)
$cancelButton.Size = New-Object System.Drawing.Size(75,23)
$cancelButton.Text = 'Cancel'
$cancelButton.DialogResult = [System.Windows.Forms.DialogResult]::Cancel
$form.CancelButton = $cancelButton
$form.Controls.Add($cancelButton)

$label = New-Object System.Windows.Forms.Label
$label.Location = New-Object System.Drawing.Point(10,20)
$label.Size = New-Object System.Drawing.Size(280,20)
$label.Text = 'Please enter the information in the space below:'
$form.Controls.Add($label)

$textBox = New-Object System.Windows.Forms.TextBox
$textBox.Location = New-Object System.Drawing.Point(10,40)
$textBox.Size = New-Object System.Drawing.Size(260,20)
$form.Controls.Add($textBox)

$form.Topmost = $true

$form.Add_Shown({$textBox.Select()})
$result = $form.ShowDialog()

if ($result -eq [System.Windows.Forms.DialogResult]::OK)
{
    $x = $textBox.Text
    $x
}
Module Linking
Main Topic Image
An overhead shot of a software engineer punch commands into their Command Line Interface (CLI)
Is Study Guide?
Off
Is Assessment Consultation?
Off