0
0
PowerShellscripting~15 mins

Select-Object for properties in PowerShell - Mini Project: Build & Apply

Choose your learning style9 modes available
Select-Object for properties
📖 Scenario: You are working with a list of files in a folder. Each file has many details like name, length, creation time, and more. You want to see only the file names and their sizes to keep things simple.
🎯 Goal: Build a PowerShell script that gets files from a folder and shows only the Name and Length properties using Select-Object.
📋 What You'll Learn
Create a variable called files that stores the list of files from the current directory using Get-ChildItem.
Create a variable called properties that holds the list of properties Name and Length.
Use Select-Object with the properties variable to select only those properties from files and store the result in selectedFiles.
Print the selectedFiles variable to show the output.
💡 Why This Matters
🌍 Real World
When managing files on your computer, you often want to see only important details like file names and sizes instead of all information.
💼 Career
IT professionals and system administrators use PowerShell scripts like this to quickly gather and report file information for maintenance and audits.
Progress0 / 4 steps
1
Get the list of files
Create a variable called files and set it to the result of Get-ChildItem to get all files in the current directory.
PowerShell
Need a hint?

Use Get-ChildItem to get files and assign it to files.

2
Create a properties list
Create a variable called properties and set it to an array containing the strings 'Name' and 'Length'.
PowerShell
Need a hint?

Use @('Name', 'Length') to create an array of property names.

3
Select only the needed properties
Use Select-Object with the properties variable to select only the Name and Length properties from files. Store the result in a variable called selectedFiles.
PowerShell
Need a hint?

Use the pipeline | to send files to Select-Object with -Property $properties.

4
Display the selected properties
Print the selectedFiles variable to show the file names and sizes.
PowerShell
Need a hint?

Simply write the variable name selectedFiles to print its content.