I wrote a script to get the mac address off of several servers, so it required my username and password. I don’t like to put my password in a script, but the credentials pop-up box for me to enter my password got a bit cumbersome. I decided to put it an a separate file for 2 reasons, 1) so my password is not saved in a script (I delete from the file once I’m done) and 2) making scripts modular is what it’s all about.

$user='domain\username'
Import-Module C:\Users\username\Documents\WindowsPowerShell\cred.ps1
$servers= @('sugar', 'milk', 'eggs', 'server7', 'buttons')

foreach ($server in $servers) {
    Get-WmiObject -ClassName Win32_NetworkAdapterConfiguration -Filter "IPEnabled='True'" -ComputerName $server | 
    Select-Object -Property MACAddress, Description
    Write-host $server
}

I wrote my script in one file and my password in a variable in another.

$pwd='$upr$trong$3cure'

To import it into my script, I added the Import-Module command and the path to my password file and ran the script. No more password prompts.

Be soical and share.