0
0
PowerShellscripting~30 mins

REST API calls with Invoke-RestMethod in PowerShell - Mini Project: Build & Apply

Choose your learning style9 modes available
REST API calls with Invoke-RestMethod
📖 Scenario: You are working with a public REST API that provides information about users. You want to fetch data from this API and process it in PowerShell.
🎯 Goal: Build a PowerShell script that calls a REST API using Invoke-RestMethod, filters the results based on a condition, and displays the filtered data.
📋 What You'll Learn
Create a variable with the API URL
Create a variable for the minimum user ID to filter
Use Invoke-RestMethod to get data from the API
Filter users with IDs greater than or equal to the minimum ID
Print the filtered users' names and IDs
💡 Why This Matters
🌍 Real World
Calling REST APIs is common when working with web services to get or send data automatically.
💼 Career
Many IT and automation jobs require scripting to interact with APIs for monitoring, reporting, or integrating systems.
Progress0 / 4 steps
1
Set the API URL
Create a variable called apiUrl and set it to the string "https://jsonplaceholder.typicode.com/users".
PowerShell
Need a hint?

Use $apiUrl = "https://jsonplaceholder.typicode.com/users" to store the URL.

2
Set the minimum user ID filter
Create a variable called minUserId and set it to the number 5.
PowerShell
Need a hint?

Use $minUserId = 5 to set the filter value.

3
Fetch and filter users
Use Invoke-RestMethod with $apiUrl to get the users and store them in a variable called users. Then create a variable called filteredUsers that contains only users with id greater than or equal to $minUserId.
PowerShell
Need a hint?

Use $users = Invoke-RestMethod -Uri $apiUrl to get data.
Use Where-Object { $_.id -ge $minUserId } to filter.

4
Display filtered users
Use a foreach loop to print each user's id and name from $filteredUsers in the format: User ID: [id], Name: [name].
PowerShell
Need a hint?

Use foreach ($user in $filteredUsers) { Write-Output "User ID: $($user.id), Name: $($user.name)" } to print.