📖 Scenario: You are learning how PowerShell works behind the scenes. Unlike many scripting languages that work with plain text, PowerShell works with objects. This means it can handle data with properties and methods, just like real-world things.Imagine you have a list of files, and each file is like a little object with details like its name, size, and creation date. PowerShell lets you easily see and use these details because it treats files as objects.
🎯 Goal: Build a simple PowerShell script that shows how PowerShell treats files as objects. You will list files in a folder, access their properties, and call a method on one of the file objects.
📋 What You'll Learn
Create a variable called files that stores the list of files in the current directory using Get-ChildItem.
Create a variable called firstFile that stores the first file object from files.
Access the Name and Length properties of firstFile.
Call the ToUpper() method on the Name property of firstFile.
Print the original file name, file size, and the uppercased file name.
💡 Why This Matters
🌍 Real World
System administrators often need to manage files and folders. PowerShell's object-oriented nature lets them easily get file details and perform actions based on those details.
💼 Career
Understanding that PowerShell works with objects helps you write scripts that are clear, powerful, and maintainable, a key skill for IT professionals and automation engineers.
Progress0 / 4 steps
1
Get the list of files as objects
Create a variable called files and set it to the result of Get-ChildItem to get the list of files in the current directory.
PowerShell
Hint
Use Get-ChildItem without parameters to get files in the current folder.
2
Select the first file object
Create a variable called firstFile and set it to the first item in the files collection using [0] index.
PowerShell
Hint
Use files[0] to get the first file object.
3
Access properties of the file object
Create two variables called fileName and fileSize. Set fileName to firstFile.Name and fileSize to firstFile.Length.
PowerShell
Hint
Use dot notation to get properties from the object.
4
Call a method and print the results
Create a variable called upperName and set it to the result of calling ToUpper() method on fileName. Then print fileName, fileSize, and upperName using Write-Output.
PowerShell
Hint
Use ToUpper() method on the string and Write-Output to print.
Practice
(1/5)
1. Why is PowerShell considered an object-oriented scripting language?
easy
A. Because it uses objects with properties and methods to handle data
B. Because it only works with text files
C. Because it requires classes to run scripts
D. Because it cannot handle variables
Solution
Step 1: Understand what object-oriented means in PowerShell
PowerShell treats data as objects that have properties (data) and methods (actions).
Step 2: Identify why this matters
This allows scripts to easily access and manipulate data in a structured way.
Final Answer:
Because it uses objects with properties and methods to handle data -> Option A
Quick Check:
Object-oriented = uses objects with properties and methods [OK]
Hint: Objects have properties and methods, not just text [OK]
Common Mistakes:
Thinking PowerShell only works with text
Believing classes are always required
Assuming variables cannot hold objects
2. Which of the following is the correct way to access the 'Length' property of a string object in PowerShell?
easy
A. $string.Length()
B. $string.Length
C. Length($string)
D. $string->Length
Solution
Step 1: Recall property access syntax in PowerShell
Properties are accessed using dot notation without parentheses.
Step 2: Apply to 'Length' property
Use $string.Length to get the length of the string.
Final Answer:
$string.Length -> Option B
Quick Check:
Properties use dot notation without () [OK]
Hint: Use dot and property name without parentheses [OK]
Common Mistakes:
Adding parentheses to properties
Using function-like syntax for properties
Using arrow notation which is not valid
3. What will be the output of this PowerShell code?
$date = Get-Date $date.DayOfWeek
medium
A. The current day of the week as a string, like 'Monday'
B. An error because DayOfWeek is not a property
C. The current date in MM/DD/YYYY format
D. The time part of the date only
Solution
Step 1: Understand Get-Date output
Get-Date returns a DateTime object with many properties including DayOfWeek.
Step 2: Access DayOfWeek property
$date.DayOfWeek returns the day of the week as a string like 'Monday'.
Final Answer:
The current day of the week as a string, like 'Monday' -> Option A
Quick Check:
DayOfWeek property returns day name [OK]
Hint: Get-Date returns object; DayOfWeek is a property [OK]
D. Using parentheses after 'Length' property causes an error
Solution
Step 1: Check property access syntax
Properties like 'Length' are accessed without parentheses in PowerShell.
Step 2: Identify the error
$file.Length() tries to call 'Length' as a method, causing an error.
Final Answer:
Using parentheses after 'Length' property causes an error -> Option D
Quick Check:
Properties do not use () [OK]
Hint: Properties never have () after them [OK]
Common Mistakes:
Adding () to properties
Thinking Get-Item can't get file info
Believing Write-Output can't print variables
5. You want to list all running processes and display their names and memory usage using PowerShell objects. Which script correctly uses object properties to do this?
hard
A. Get-Process | ForEach-Object { '$_.Name uses $_.WorkingSet64 bytes' }
B. Get-Process | ForEach-Object { "$_.Name uses $_.WorkingSet64() bytes" }
Step 1: Understand property access and string concatenation
Properties are accessed with dot notation without parentheses. Concatenate strings with + in PowerShell.
Step 2: Analyze each option
A uses quotes with variables inside strings incorrectly (no expansion). B uses () on property causing error. C uses invalid arrow notation. D correctly concatenates properties and strings.