14 October 2016

PowerShell: Assign Email Address to Environment Variable

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:    

Related Posts:

  • Query Event Logs to a Central Log File Recently, we have had sporadic issues with Outlook and needed to see who all was experiencing the problem. We knew it was logged in the event viewer logs. I decided to write a PowerShell script, with the help of Sapien Tech… Read More
  • Bitlocker Access is Denied We recently started getting access is denied when trying to enable bitlocker on our Dell Latitude E7440 models. At first we thought it was a problem with the laptop itself upon the first occurrence of it. We were able to i… Read More
  • Using PowerShell to control Dell Command | Update for drivers and BIOS updates The Dell Command | Update is a great tool to use, but it also lacks some features that I have implemented in the following script. During system builds, I have the application to update all drivers and the BIOS. The feature… Read More
  • Import Active Directory Module into Windows PE One thing I have been wanting to have is access to active directory in a WinPE environment. The main reason I want it is to be able to delete systems from active directory during a build. When I first started researching, I… Read More
  • Apple QuickTime Uninstaller As we have all seen recently, it is time we get rid of Apple QuickTime from PCs as they are no longer going to update the software. I had a few ask me to publish a blanket uninstaller for QuickTime. With the help of Sapien'… Read More

0 comments:

Post a Comment