Skip to content

Implement Export command to allow setting of environment variables in a bash-like way #20475

@JohnLBevan

Description

@JohnLBevan

Summary of the new feature / enhancement

As a scripter I want to be able to run export DEMO_CLIENT_ID="ff3b7b84-e041-4e76-993e-239930808aa7" in powershell and have it update the DEMO_CLIENT_ID environment variable with a value of ff3b7b84-e041-4e76-993e-239930808aa7 as it would in Bash.

PowerShell has been written to mirror shell script in many cases, often providing aliases for PowerShell cmdlets to allow the shell script syntax to be used without modification.
One of the most common things we see in sample code / documentation is the use of Linux export statement; often this documentation doesn't have the PowerShell equivalent (e.g. TerraForm's docs) meaning examples are harder to follow / require more effort to amend syntax. Having native support for an export command would save many users effort.

Proposed technical implementation details (optional)

Something similar to the below would be good. Note: this is a quick version that I knocked up for personal use; it's not yet a perfect like-for-like with the linux/mac shell script equivalent; but works in the majority of simple use cases.

Function Export {
    $regex = [System.Text.RegularExpressions.Regex]::new('^\s*([^=]+?)\s*=(.*)$')
    $result = $regex.Match(($args -join ' '))
    if (!$result.Success) {
        throw "Could not set an environment variable based on [$($args -join ' ')]"
    }
    [Environment]::SetEnvironmentVariable($result.Groups[1].Value, $result.Groups[2].Value)
}

Side note: I've also posted this function to StackExchange's CodeReview, so improved versions or feedback on the above may be available.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Issue-Enhancementthe issue is more of a feature request than a bugResolution-DeclinedThe proposed feature is declined.WG-Cmdletsgeneral cmdlet issuesWG-ReviewedA Working Group has reviewed this and made a recommendation

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions