LogoLogo
TwitterLinkedInGitHubPowerShell Gallery
  • About
  • Events
  • Blog
    • 2022
      • 🆕OSD January Update
      • 🆕PSCloudScript Basics
    • 2021
      • Start-OOBEDeploy
      • OSDCloud
      • PowerShell Gallery in WinPE
      • BitLocker KeyProtectors
      • WindowsCapability -and WindowsPackage
      • Scheduled Tasks
        • Building a Task
        • Task Permissions
        • Task Trigger
        • Action a PowerShell File
        • Action a PS Encoded Script
        • Conclusion
          • Windows Activation and Edition Change
          • REG.exe and Multiple Actions
    • 2019
      • 2019-02
        • Offline Servicing vs Reference Image
      • 2019-04
        • Windows 10 Upgrade MultiLang (Uno)
      • 2019-06
        • Offline Servicing Windows 10 with CU for .NET 4.8
        • OSDBuilder and .NET CU KB4480056
      • 2019-09
        • Black Screen During Windows 10 Setup
      • 2019-11
        • I Hate OSDBuilder
    • 2018
      • WinPE 10 1809 WPF DLL Fix
      • Microsoft Update Releases
      • Create WinPE.wim from Boot.wim or WinRE.wim
      • Windows Setup: FAT32 USB with +4GB Install.wim
      • Windows 10 from ESD
      • Windows 10 1809 Appx Issues
      • Mount-WindowsImage -Optimize
  • Guides
    • Autopilot App Registration
    • PSCloudScript
      • PS Cmdlets
      • GitHub Gist
      • GitHub Git Repo
      • Content-Type | Azure Static Web App
      • Command Shortening
      • Azure Key Vault Secret
      • OSD PowerShell Module
      • PSCloudScript Examples
        • Autopilot
        • AutopilotOOBE
        • OSDCloud Live
        • WinPE PowerShell Gallery
        • OSDCloud WinPE and OOBE
    • go OSDCloud
      • Azure Function
      • Custom Domain
      • SSL Binding
      • Proxies
  • PowerShell
    • OSD
    • OSDCloud
    • OSDBuilder (Offline Servicing)
    • OSDSUS (Update Catalogs)
    • OSDUpdate (MS Updates)
    • OSDDrivers (Compact Drivers)
    • PShot
      • Release Notes
      • Usage
        • -Directory
        • $AutoPath
        • -Prefix
        • -Count
        • -Delay
        • -Clipboard
        • -Primary
        • The Object
      • Technical
        • Why a Module?
        • Resolution, Scale and DPI
Powered by GitBook
On this page
  • Invoke-RestMethod
  • Invoke-Expression
  • Order
  • Sponsor
  1. Guides
  2. PSCloudScript

PS Cmdlets

22.1.16

PreviousPSCloudScriptNextGitHub Gist

Last updated 3 years ago

The two PowerShell Cmdlets that are used are Invoke-RestMethodand Invoke-Expression. It is also important that these cmdlets are processed in this order, which I will touch on at the bottom of this page

Invoke-RestMethod

This cmdlet is used to return the contents of a provided URL in a structured, typically Raw format

Here are some examples of how to use Invoke-RestMethod to get the raw content from a Url

PS C:\> Invoke-RestMethod -Uri https://raw.githubusercontent.com/OSDeploy/PSCloudScript/main/ps-cmdlets.txt
This is a test to see if this raw content can be saved as a PowerShell string

PS C:\> irm https://raw.githubusercontent.com/OSDeploy/PSCloudScript/main/ps-cmdlets.txt
This is a test to see if this raw content can be saved as a PowerShell string

PS C:\> $String = irm raw.githubusercontent.com/OSDeploy/PSCloudScript/main/ps-cmdlets.txt
PS C:\> $String
This is a test to see if this raw content can be saved as a PowerShell string

Invoke-Expression

This cmdlet is used to execute a PowerShell Command. You can read Microsoft's documentation here

If you read the Microsoft Doc, you'll understand that iex commands can come from a String. This can be easily demonstrated in this example

PS D:\> $String = @'
Write-Host 'This is PowerShell String'
'@

PS D:\> Invoke-Expression -Command $String
This is PowerShell String

PS D:\> Invoke-Expression $String
This is PowerShell String

PS D:\> iex $String
This is PowerShell String

This method even works if there is a PowerShell function in the String. Keep in mind this function is only available in the current PowerShell session

PS D:\> $StringFunction = @'
function Test-PowerShell
{
    [CmdletBinding()]
    param()
    Write-Host 'This is a PowerShell Function'
    Write-Verbose 'And this is a Verbose PowerShell Function'
}
'@

PS D:\> Invoke-Expression -Command $StringFunction

PS D:\> Test-PowerShell
This is a PowerShell Function

PS D:\> Test-PowerShell -Verbose
This is a PowerShell Function
VERBOSE: And this is a Verbose PowerShell Function

Order

These commands have to follow a specific order. You need the content from Invoke-RestMethod before you can execute in Invoke-Expression

#Command in parenthesis is processed first in both of these examples
Invoke-Expression -Command (Invoke-RestMethod -Uri $Uri)
#Alternate using Alias
iex(irm $Uri)

#Pipeline can be used
Invoke-RestMethod -Uri $Uri | Invoke-Expression
#Alternate using Alias
irm $Uri | iex

Sponsor

OSDeploy is sponsored by and their Systems Management Tools

Recast Software
Invoke-RestMethod (Microsoft.PowerShell.Utility) - PowerShelldocsmsft
Logo
Invoke-Expression (Microsoft.PowerShell.Utility) - PowerShelldocsmsft
Logo
HomeRecast Software
Sponsored by Recast Software
Logo