Creating Your First PowerShell Script Using PowerShell Core

45 minutes
  • 3 Learning Objectives

About this Hands-on Lab

In this hands-on lab, you will create a PowerShell script that outputs process information to a file. You will have to use the appropriate PowerShell commands to filter the processes that are consuming more than 2 MB of memory. Additionally, you will be required to output only the `WorkingSet` and `ProcessName` columns to the file. Once you’ve created the file, you can view the contents using the `Get-Content` cmdlet.

Learning Objectives

Successfully complete this lab by achieving the following learning objectives:

Connect to the Linux VM and Create the File
  1. Use the public IP and credentials from the lab instructions page to connect to the VM:

    ssh cloud_user@<PUBLIC_IP>
  2. Use a text editor (e.g., Vim) to create a file named processinfo.ps1.

Create the PowerShell Script
  1. Within the processinfo.ps1 file, type in the command Get-Process and store it in the variable $ps.
  2. Use pipelines to pass the output of Get-Process to Where-Object and only take the WorkingSet object type.
  3. Use pipelines again to pass the output of the WorkingSet object to Format-Table. Only output the WorkingSet column and the ProcessName column, and store everything in the variable $psinfo.
  4. Use pipelines one final time to output the file (using out-file) to a CSV file named processinfo.csv.
  5. Save the file and exit Vim (hint: enter :wq!).
Run the Script and Verify the Contents
  1. Run the PowerShell script by typing in ./processinfo.ps1 from the same directory as the file.
  2. Open the file using the Get-Content cmdlet.

Additional Resources

You are required to provide your manager with a report of all processes that consume more than 2 MB of memory. The report must be in a formatted table and contain only the process name and the consumed amount (in KB).

You have been provided a Linux VM with this hands-on lab, into which you must first log in to complete the following tasks:

  • Connect to the Linux VM via SSH using the credentials provided with this hands-on lab.
  • Create a PowerShell script named processinfo.ps1.
  • In the script, enter the appropriate commands to obtain the list of processes and export to a file named processinfo.csv.
  • Run the script from the PowerShell prompt and verify the contents of the file.

Note: Even though the file name is "processinfo.csv", this will not output a comma-separated value (CSV) file

What are Hands-on Labs

Hands-on Labs are real environments created by industry experts to help you learn. These environments help you gain knowledge and experience, practice without compromising your system, test without risk, destroy without fear, and let you learn from your mistakes. Hands-on Labs: practice your skills before delivering in the real world.

Sign In
Welcome Back!

Psst…this one if you’ve been moved to ACG!

Get Started
Who’s going to be learning?