In PowerShell, if you’re encountering a 401 Unauthorized error after approximately an hour, it’s likely due to the expiration of the authentication token you’re using. For SAML2 authentication with Microsoft services, the token typically has a lifespan of one hour, after which it needs to be refreshed.

Standard script to check token validation.
Follow for How to create Application Registration in Azure ?


- Retrieve the Token: First, obtain the access token you want to verify. You might receive this token during authentication or authorization processes.
- Decode the Token: Use a tool like JWT.IO to decode the token. This website allows you to paste the token and see its contents. The token consists of three parts: header, payload, and signature.
What are JWT Tokens?
- JSON Web Tokens (JWTs) are a standardized way to securely transmit data between parties. They contain claims (information) encoded in JSON format.
- JWTs are signed using cryptography to ensure data authenticity. However, they are not encrypted, so HTTPS is crucial for secure transmission.
- While JWTs guarantee data ownership, they don’t provide encryption. Use them with HTTPS to enhance security.

Token contain an hour expire time window. here is your problem exist if it is crossing one hour window runtime.
To get rid of Timeout happening at hour.
Follow Part-2 for Lifetime Token Policy Creation.
Set lifetimes for tokens – Microsoft identity platform | Microsoft Learn