Get Folder Path of the Current Script
Function Get-ScriptDirectory{ $Invocation = (Get-Variable MyInvocation -Scope 1).Value; if($Invocation.PSScriptRoot){ $Invocation.PSScriptRoot; } elseif($Invocation.MyCommand.Path){ Split-Path $Invocation.MyCommand.Path } elseif($Invocation.InvocationName.LastIndexOf("\") -gt 0){ $Invocation.InvocationName.Substring(0,$Invocation.InvocationName.LastIndexOf("\")); } else { Get-PSDrive | Where-Object{$_.Provider.Name -eq 'FileSystem'} | Foreach-Object { Join-Path -Path $_.Root -ChildPath $_.CurrentLocation } } }
Get File Name and Extension from Path
# Return just the file name [System.IO.Path]::GetFileName($FilePath) # Returns file name without the extension [System.IO.Path]::GetFileNameWithoutExtension($FilePath) # Returns the file extension only [System.IO.Path]::GetExtension($FilePath)
Grant Permissions to Folder Share
Grant-SmbShareAccess -name $ShareName -AccountName $Account -AccessRight Full -Force
Revoke All Permissions From Folder Share
Get-SmbShareAccess -name $ShareName | Foreach {Revoke-SmbShareAccess -name $ShareName -AccountName $_.AccountName -Force}
Create a Folder Share
$Shares=[WMICLASS]'WIN32_Share' $sd = ([WMIClass] "Win32_SecurityDescriptor").CreateInstance() $shares.create($FolderPath, $ShareName, 0, 100, "Description", "", $sd)
Create Folder If It Doesn’t Exist
If (-not(test-path $FolderPath)){ New-Item -type directory -Path $FolderPath }
Get FSMO Roles
# Get the Domain Level Roles Get-ADDomain | Select-Object InfrastructureMaster, RIDMaster, PDCEmulator # Get the Forest Level Roles Get-ADForest | Select-Object DomainNamingMaster, SchemaMaster
Search for AD User by Partial Name
Get-ADUser -Filter {Name -like '*joe*'}
Search for AD User without AD module
# search based on SamAccountNamer $strFilter = "(SAMAccountName=$username)" $objDomain = New-Object System.DirectoryServices.DirectoryEntry $objSearcher = New-Object System.DirectoryServices.DirectorySearcher $objSearcher.SearchRoot = $objDomain $objSearcher.PageSize = 1000 $objSearcher.Filter = $strFilter $objSearcher.SearchScope = "Subtree" # Add additional properties to return here $colProplist = "name","SAMAccountName" foreach ($i in $colPropList){ $foo = $objSearcher.PropertiesToLoad.Add($i) } $colResults = $objSearcher.FindAll() # formation output results [System.Collections.Generic.List[PSObject]] $results = @() foreach ($objResult in...
Test AD User Credentials
Add-Type -AssemblyName System.DirectoryServices.AccountManagement $DS = New-Object System.DirectoryServices.AccountManagement.PrincipalContext('domain') $DS.ValidateCredentials($UserName, $Password)
