Skip to content

Instantly share code, notes, and snippets.

View davegreen's full-sized avatar

Dave Green davegreen

View GitHub Profile
@davegreen
davegreen / UPN.ps1
Created May 29, 2014 14:42
Quick one-liner for setting UPNs for users to match their email address (useful for working with Azure, O365, etc.)
#Set UPN for all users with mobile phone numbers to email address
Get-ADuser -Filter * -Properties mobilephone, mail | where {$_.mobilephone -and $_.mail -ne $_.userPrincipalName} | ForEach-Object {Set-ADuser -Identity $_.ObjectGUID -UserPrincipalName $_.mail}
#Set UPN for all users to email address
Get-ADuser -Filter * -Properties mail | where {$_.mail -ne $_.userPrincipalName} | ForEach-Object {Set-ADuser -Identity $_.ObjectGUID -UserPrincipalName $_.mail}
/// <summary>
/// Gets the DirectoryEntry object of the LDAP distinguishedname passed in, using the current user credentials.
/// </summary>
/// <param name="path">A distinguishedname of the object you wish to get the DirectoryEntry for.</param>
/// <returns></returns>
private static DirectoryEntry GetDirectoryEntry(string path)
{
DirectoryEntry de = new DirectoryEntry();
de.Path = path;
de.AuthenticationType = AuthenticationTypes.Secure;
@davegreen
davegreen / Get-ADDate.ps1
Last active December 19, 2015 12:39
A function that can be passed a date from AD, such as the lastlogon attribute and returns a string of the date, or the string 'Never'.
# Returns a date string based on an AD attribute (like lastlogon, or accountexpires).
# Param1: $addate - AD attribute date value.
Function Get-ADDate($addate)
{
Try
{
$formatteddate = [datetime]::FromFileTime($addate).Date.ToString()
}
Catch
@davegreen
davegreen / Get-Membership.ps1
Last active December 18, 2015 20:49
A quick function to get groups that the current user is a member of.
# Gets a list of groups that the user is a member of from the current Windows identity token.
# Param1: $domain - Boolean value. Return group names with domain or not.
# e.g. true = "BUILTIN\Authenticated Users", false = "Authenticated Users".
Function Get-Membership($domain)
{
$groups = @()
foreach ($group in [System.Security.Principal.WindowsIdentity]::GetCurrent().Groups)
{
$grp = $group.Translate([System.Security.Principal.NTAccount]).ToString()