0
0
PowerShellscripting~20 mins

Automatic variables ($_, $PSVersionTable) in PowerShell - Mini Project: Build & Apply

Choose your learning style9 modes available
Explore PowerShell Automatic Variables $_ and $PSVersionTable
📖 Scenario: You are working with PowerShell to process a list of files and want to learn how to use automatic variables to make your scripts simpler and more powerful.
🎯 Goal: Learn to use the automatic variable $_ inside a loop to access the current item, and explore the $PSVersionTable automatic variable to check your PowerShell version.
📋 What You'll Learn
Create an array of file names
Use the automatic variable $_ inside a loop to print each file name
Access the $PSVersionTable automatic variable to display PowerShell version info
Print the results clearly
💡 Why This Matters
🌍 Real World
PowerShell scripts often process lists of files or data items. Using automatic variables like $_ makes it easy to write concise and readable scripts that handle each item automatically.
💼 Career
Understanding automatic variables is essential for system administrators and automation engineers who write PowerShell scripts to manage systems, automate tasks, and gather system information efficiently.
Progress0 / 4 steps
1
Create an array of file names
Create a variable called $files and assign it an array with these exact file names: 'report.docx', 'data.csv', 'image.png', 'notes.txt'.
PowerShell
Need a hint?

Use @( ... ) to create an array in PowerShell.

2
Set up a loop to process each file
Add a foreach loop that goes through each item in $files using the automatic variable $_ inside the loop body.
PowerShell
Need a hint?

Use ForEach-Object { ... } and inside the braces, $_ is the current item.

3
Print each file name using $_
Inside the ForEach-Object loop, add a Write-Output command to print the current file name stored in $_.
PowerShell
Need a hint?

Use Write-Output $_ to print the current item.

4
Display PowerShell version using $PSVersionTable
Add a line to print the PowerShell version information stored in the automatic variable $PSVersionTable.
PowerShell
Need a hint?

Use Write-Output $PSVersionTable to show version details.