[MSH] Creating System Variable

Discussion in 'Scripting' started by /\\/\\o\\/\\/, Nov 2, 2005.

  1. I want to do the EQ of this te create a system variable from MSH :

    Set Env = WshShell.Environment("SYSTEM")
    Env("NewVar") = "Val"

    but in MSH I get a string back.

    MSH > $env = (new-object -com "WScript.Shell").Environment("SYSTEM")
    MSH> $env gm
    TypeName: System.String

    so I can not set it.

    B.T.W. for setting a SystemVariable (as Path) you can use WMI
    but not for creating.

    $env = get-WMIObject win32_environment -filter "Name = 'Path'"
    $env.VariableValue = $env.VariableValue + ";c:\test"

    gr /\/\o\/\/
    /\\/\\o\\/\\/, Nov 2, 2005
    1. Advertisements

  2. I found a work-around :

    (I almost give te solution in the last post Setting the Path / system
    variables from MSH )
    first create the Key, then Set it using WMI (so it gets updated ;-).

    new-property -Path "HKLM:\System\CurrentControlSet\Control\Session
    manager\Environment" -Property New -Type string -Value "foo"
    $env = get-WMIObject win32_environment -filter "Name = 'New'"
    $env.VariableValue = $env.VariableValue + "Val"

    gr /\/\o\/\/
    /\\/\\o\\/\\/, Nov 2, 2005
    1. Advertisements

  3. /\\/\\o\\/\\/

    Keith Hill Guest

    Take a look at .NET's System.Environment.SetEnvironmentVariable() methods.
    One of the overloads takes an enum that lets you specify User or System
    environment variable.
    Keith Hill, Nov 3, 2005
  4. MSH> $env = (new-object -com "WScript.Shell").Environment("SYSTEM")
    MSH> ,$env |gm
    TypeName: System.__ComObject#{f935dc29-1cf0-11d0-adb9-00c04fd58a0b}

    Notice the leading ",". The problem that you had was the $env is ENUMERABLE
    so putting it into the pipeline unrolls it into its parts (strings), By
    putting a "," in front you are creating a LIST of 2 elements
    1) $NULL
    2) $ENV (the IENUMERABLE)
    This list gets unrolled and passes on its parts ($ENV) so that GM reports

    The less esoteric way to address this is:
    MSH> gm -input $env

    In any case, the Setter works:
    MSH> $env.item("TMP") = "c:\temp"
    Jeffrey Snover [MSFT], Nov 3, 2005
  5. thx, this also solves may other open Post :

    [MSH] Get-member and Collections / DataTables.
    this works :

    ,$dt | gm

    I realy missed out on that one !
    I did And yes this Works,


    thx Both /\/\o\/\/
    /\\/\\o\\/\\/, Nov 3, 2005
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.