For the complete documentation index, see llms.txt. This page is also available as Markdown.

Cache

PowerShell Universal provides a server-level cache for sharing data between APIs, automation, and apps. Set cache entries with Set-PSUCache and retrieve them with Get-PSUCache.

Cache values are serialized with the PowerShell serializer. Retrieved objects are deserialized objects rather than live instances.

Set-PSUCache -Key 'CurrentDate' -Value (Get-Date)
Get-PSUCache -Key 'CurrentDate'

Expiration

Cache items support absolute, relative, and sliding expiration.

Set-PSUCache -Key 'CurrentDate' -Value (Get-Date) -AbsoluteExpiration (Get-Date).AddMinutes(10)
Set-PSUCache -Key 'CurrentDate' -Value (Get-Date) -AbsoluteExpirationFromNow ([TimeSpan]::FromHours(1))
Set-PSUCache -Key 'CurrentDate' -Value (Get-Date) -SlidingExpiration ([TimeSpan]::FromMinutes(5))

Persistent cache

Use -Persist to store a cache entry in the PowerShell Universal database. Persistent entries survive service restarts and are available to connected servers that use the same database.

Set-PSUCache -Key 'CurrentDate' -Value (Get-Date) -Persist

Encrypt persistent values

Use -Encrypt with -Persist to encrypt the serialized cache value before it is stored in the database. PowerShell Universal decrypts the value automatically when Get-PSUCache retrieves it.

Set-PSUCache -Key 'ApiResponse' -Value $response -Persist -Encrypt

$response = Get-PSUCache -Key 'ApiResponse'

-Encrypt requires -Persist; it cannot encrypt memory-only cache entries. Use encrypted persistent cache for sensitive values that must be shared across servers or retained after restarts. Continue to limit access to sensitive cache data with appropriate permissions and reader roles.

Remove cache entries

$Cache scope

APIs, automation scripts, and dashboards support $Cache: variables for in-memory data shared across runspaces in the same execution environment.

$Cache: is separate from the server-level cache. It is limited to the execution environment; for example, a dashboard cache value is not available to an API unless both use the Integrated environment.

View the cache

View server-level cache entries under Manage > Cache.

API

  • Get-PSUCache

  • Set-PSUCache

  • Remove-PSUCache

  • Clear-PSUCache

Last updated

Was this helpful?