0
0
PowerShellscripting~30 mins

PowerShell on Linux - Mini Project: Build & Apply

Choose your learning style9 modes available
PowerShell on Linux: Basic File Info Script
📖 Scenario: You are using PowerShell on a Linux system to check details about files in a directory. This is useful when you want to quickly see file names and sizes without switching to other tools.
🎯 Goal: Create a PowerShell script that lists files in a directory and shows their names and sizes in bytes.
📋 What You'll Learn
Create a variable with a directory path
Create a variable to hold a file size limit
Use a loop to get files and filter by size
Print the file name and size for each filtered file
💡 Why This Matters
🌍 Real World
This script helps Linux users who use PowerShell to quickly find small files in a directory, useful for managing disk space or checking file details.
💼 Career
Knowing how to use PowerShell on Linux is valuable for system administrators and developers working in mixed environments or automating tasks across platforms.
Progress0 / 4 steps
1
Set the directory path
Create a variable called $directory and set it to the string "/usr/bin".
PowerShell
Need a hint?

Use = to assign the string "/usr/bin" to the variable $directory.

2
Set the file size limit
Create a variable called $sizeLimit and set it to the number 10000 (this means 10,000 bytes).
PowerShell
Need a hint?

Assign the number 10000 to the variable $sizeLimit.

3
Get files and filter by size
Use Get-ChildItem with the path $directory to get files. Use a foreach loop with variable $file to check each file's Length. Inside the loop, use an if statement to select files where $file.Length -lt $sizeLimit. For these files, create a new object with properties Name and Length and add it to a list called $smallFiles.
PowerShell
Need a hint?

Start with an empty array $smallFiles = @(). Loop over files with foreach ($file in Get-ChildItem -Path $directory). Use if ($file.Length -lt $sizeLimit) to filter. Add matching files as custom objects to $smallFiles.

4
Display the filtered files
Use foreach with variable $item to loop over $smallFiles. Inside the loop, use Write-Output to print the file name and size in this format: "File: {Name}, Size: {Length} bytes".
PowerShell
Need a hint?

Loop over $smallFiles and print each file's name and size using Write-Output with string interpolation.