Intro to PowerShell Lab Scripting with Powershell
Learning objective: By the end of this exercise, students will be able to write and execute a simple PowerShell script to automate a routine task.
Scripting is where the true power of PowerShell shines. By combining the commands you’ve learned so far, you can create scripts to automate repetitive tasks and streamline your workflow. Let’s create a simple script to get you started. ✨
Steps
-
In your Powershell console, you should be in the
intro-to-powershell-labdirectory if you’re not already. -
Create a new file called
get-system-processes.ps1usingNew-Item -ItemType File. -
Open the current directory in Visual Studio Code using
code .. -
Add code that will implement this functionality to the script:
-
Research the
Write-Hostcmdlet to display a message reading: “Running Processes:”. -
Use the
Get-Processcmdlet to display a list of running processes. Research how to use theSelect-Objectcmdlet in combination with theGet-Processcmdlet to only show theName,Id,CPU, andWorkingSetof each process.
-
-
Ensure the script is saved and close VS Code.
-
Execute the script by running
./get-system-processes.ps1in the PowerShell console to test it.Congratulations! You’ve just written and executed your first PowerShell script. The script retrieves computer system information and displays a list of running processes. Scripts can be customized and expanded on to automate various tasks based on your needs.