Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
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
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
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
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
Hint
Loop over $smallFiles and print each file's name and size using Write-Output with string interpolation.
Practice
(1/5)
1. What command do you use to start PowerShell on a Linux system?
easy
A. pwsh
B. powershell.exe
C. start-ps
D. shellps
Solution
Step 1: Recall the PowerShell start command on Linux
On Linux, PowerShell is started by typing pwsh in the terminal.
Step 2: Compare options with the known command
powershell.exe, start-ps, and shellps are not valid commands to start PowerShell on Linux.
Final Answer:
pwsh -> Option A
Quick Check:
PowerShell start command on Linux = pwsh [OK]
Hint: Remember: 'pwsh' starts PowerShell on Linux terminals [OK]
Common Mistakes:
Typing 'powershell.exe' which is for Windows only
Using 'start-ps' which is not a valid command
Confusing shell names
2. Which of the following is the correct syntax to run a Linux command inside PowerShell on Linux?
easy
A. Run-Command 'ls -l'
B. Invoke-Linux ls -l
C. ls -l
D. Start-Linux ls -l
Solution
Step 1: Understand how Linux commands run in PowerShell on Linux
PowerShell on Linux allows running Linux commands directly by typing them as is, like ls -l.
Step 2: Evaluate each option
Invoke-Linux ls -l, Run-Command 'ls -l', and Start-Linux ls -l are not valid syntax to run Linux commands.
Final Answer:
ls -l -> Option C
Quick Check:
Run Linux commands directly in PowerShell = ls -l [OK]
Hint: Run Linux commands directly without extra syntax in PowerShell [OK]
Common Mistakes:
Adding unnecessary PowerShell cmdlets before Linux commands
Using quotes incorrectly around Linux commands
Assuming Linux commands need special wrappers
3. What will be the output of this PowerShell on Linux command sequence?
But it returns an error: "Get-Process: The term 'Get-Process' is not recognized." What is the likely cause?
medium
A. PowerShell is not installed correctly on Linux
B. You are running the script in the Linux shell, not inside PowerShell
C. The process 'bash' does not exist
D. Get-Process cmdlet is not available on Linux PowerShell
Solution
Step 1: Analyze the error message
The error says 'Get-Process' is not recognized, which usually means the command is run outside PowerShell.
Step 2: Understand environment context
If you run PowerShell commands in the Linux shell (bash), they won't work. You must be inside PowerShell (started with pwsh) to run Get-Process.
Final Answer:
You are running the script in the Linux shell, not inside PowerShell -> Option B
Quick Check:
Run PowerShell cmdlets inside pwsh shell [OK]
Hint: Run PowerShell commands only inside pwsh shell, not bash [OK]
Common Mistakes:
Assuming Get-Process is missing on Linux PowerShell
Thinking the process 'bash' does not exist
Not starting PowerShell before running commands
5. You want to write a PowerShell script on Linux that lists all running processes and filters only those owned by the current user. Which approach is correct?
hard
A. Get-Process | Where-Object { $_.UserName -eq $env:USER }
B. ps -u $USER | pwsh
C. Get-Process | Where-Object { $_.UserName -eq (whoami) }
D. Get-Process | Where-Object { $_.UserName -eq $env:USERNAME }
Solution
Step 1: Identify environment variable for current user in PowerShell on Linux
PowerShell on Linux uses $env:USER to get the current user's name.
Step 2: Check filtering syntax for processes
Filtering processes by Where-Object { $_.UserName -eq $env:USER } correctly compares the process owner to the current user.
Step 3: Evaluate other options
ps -u $USER | pwsh mixes Linux command with PowerShell incorrectly. Get-Process | Where-Object { $_.UserName -eq (whoami) } uses command output that may not match precisely. Get-Process | Where-Object { $_.UserName -eq $env:USERNAME } uses $env:USERNAME which is typically not set on Linux.