79209817

Date: 2024-11-21 05:48:36
Score: 0.5
Natty:
Report link

Based on @mklement0's answer and with the same caveats, here's an in-memory solution that doesn't require serialization:

# Make a back-up of the environment variables
$env_bak = @(Get-ChildItem env:)
    
... my stuff ...
    
# Restore environment variables
Get-ChildItem env: | Remove-Item
$env_bak | % { Set-Item "env:$($_.Name)" $_.Value }

Operating on all of the environment variables seems like a risky proposition, so here's an alternative, filtering only those variables of interest:

# Make a back-up of selected environment variables
$env_bak_vars = @("MY_VAR", "THAT_THING", "PROJECT_DIR")
$env_bak = @(Get-ChildItem env: | Where-Object Name -CIn $env_bak_vars)
    
... my stuff ...
    
# Restore selected environment variables
Get-ChildItem env: | Where-Object Name -CIn $env_bak_vars | Remove-Item
$env_bak | % { Set-Item "env:$($_.Name)" $_.Value }
Reasons:
  • Long answer (-0.5):
  • Has code block (-0.5):
  • User mentioned (1): @mklement0's
  • Low reputation (0.5):
Posted by: pupitetris