How to create a variable from hashtable value

Welcome Forums General PowerShell Q&A How to create a variable from hashtable value

This topic contains 1 reply, has 2 voices, and was last updated by

 
Keymaster
7 months ago.

  • Author
    Posts
  • #99190

    Participant
    Points: 1
    Rank: Member

    Hi team.
    I'm trying to create a variable from a hashtable value. If he logged on user matches the hashtable key, create the variable off the hashtable value.

    # Create hashtable
    
    $environments = @{  
    'john.davies' = "IPC_john.davies"
    }
     
    # Variable for current user.
    $env:UserName
    
    # Variable I'm trying to create. I'm not sure how to get it to return the associated value and create the variable from it.
    $DeviceName = If ($environments.ContainsKey($env:UserName))
    
  • #99196

    Keymaster
    Points: 1,638
    Helping HandTeam Member
    Rank: Community Hero

    You refer to hash table values by their key.

    $environments['John.davies']

    would return

    "IPC_john.davies"

The topic ‘How to create a variable from hashtable value’ is closed to new replies.