0
0
PowerShellscripting~15 mins

Why PowerShell is object-oriented - See It in Action

Choose your learning style9 modes available
Why PowerShell is Object-Oriented
📖 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
Need a 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
Need a 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
Need a 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
Need a hint?

Use ToUpper() method on the string and Write-Output to print.