> For the complete documentation index, see [llms.txt](https://www.osdeploy.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://www.osdeploy.com/guides/pscloudscript/ps-cmdlets.md).

# PS Cmdlets

The two PowerShell Cmdlets that are used are `Invoke-RestMethod`and `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

{% embed url="<https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/invoke-restmethod?view=powershell-5.1>" %}

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

```powershell
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

{% embed url="<https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/invoke-expression?view=powershell-5.1>" %}

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

```powershell
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

```powershell
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 [Recast Software](https://www.recastsoftware.com/?utm_source=osdeploy\&utm_medium=ad\&utm_campaign=web) and their Systems Management Tools

{% embed url="<https://www.recastsoftware.com/?utm_source=osdeploy&utm_medium=ad&utm_campaign=web>" %}
Sponsored by Recast Software
{% endembed %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://www.osdeploy.com/guides/pscloudscript/ps-cmdlets.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
