14 October 2016

PowerShell: Assign Email Address to Environment Variable

Posted By: Mick Pletcher - 10:42 AM

Share

& Comment

Recently, we encountered a special situation where we needed to have an environment variable that consisted of the user's email address. That prompted me to write the following script that will do just that. The script needs to be executed as the user. I set it up in the logon script and hid the PowerShell script. This script was executed on Windows 10 machines, so I cannot guarantee it will execute on other OS versions.

NOTE: This script requires RSAT to be installed. in order to utilize the ActiveDirectory module.


You can get the script from here.


1:  <#  
2:       .SYNOPSIS  
3:            Email Address Environment Variable  
4:         
5:       .DESCRIPTION  
6:            This script will assign the user's email address to the environment variable EmailAddress.  
7:         
8:       .NOTES  
9:            ===========================================================================  
10:            Created with:      SAPIEN Technologies, Inc., PowerShell Studio 2016 v5.2.128  
11:            Created on:        10/12/2016 1:14 PM  
12:            Created by:        Mick Pletcher  
13:            Organization:  
14:            Filename:          EmailEnvVariable.ps1
15:            ===========================================================================  
16:         
17:       .PARAMETER  
18:            A description of the parameter.  
19:  #>  
20:  [CmdletBinding()]  
21:  param ()  
22:    
23:  Import-Module ActiveDirectory  
24:  #Delete Environment Variable  
25:  [System.Environment]::SetEnvironmentVariable("EmailAddress", $null, 'User')  
26:  #Get the email address associated with the username  
27:  $EmailAddress = (Get-ADUser $env:USERNAME -Properties mail).mail  
28:  #Create a user based environment variable called email address  
29:  [System.Environment]::SetEnvironmentVariable("EmailAddress", $EmailAddress, 'User')  
30:    

About Mick Pletcher

My blog is here to help solve issues I have encountered and solved, publish scripts I have written, and educate others in understanding areas that are not well covered

0 comments:

Post a Comment

Copyright © 2013 Mick's IT Blogs™ is a registered trademark.