Skip to content

Instantly share code, notes, and snippets.

@ScriptingPro
ScriptingPro / PowerShell LDAP.ps1
Created January 4, 2018 22:33
LDAP bind to server/port with PowerShell using DirectoryEntry Class and query with DirectorySearcher Class
# define your query filter
$LDAPfilter = "(samaccountname=*)"
# define the object attributes you want returned
$Attributes = @"
samaccountname
givenname
surname
mail
@ScriptingPro
ScriptingPro / Array Collections.ps1
Created January 5, 2018 05:12
Playing with Collections
$SplitHereString = @"
a
b
c
"@ -split [System.Environment]::NewLine
$SplitHereString.GetType() # notice it's String[] / System.Array
$ConstructedArray = @('a','b','c')
@ScriptingPro
ScriptingPro / encoding-helpers.ps1
Created January 26, 2018 22:57 — forked from jpoehls/encoding-helpers.ps1
Convert-FileEncoding and Get-FileEncoding
<#
.SYNOPSIS
Converts files to the given encoding.
Matches the include pattern recursively under the given path.
.EXAMPLE
Convert-FileEncoding -Include *.js -Path scripts -Encoding UTF8
#>
function Convert-FileEncoding([string]$Include, [string]$Path, [string]$Encoding='UTF8') {
$count = 0
# directoryentry (connect to specific dc:port)
$de = ([ADSI]"LDAP://$($ADDomainController.HostName):$($ADDomainController.LdapPort)")
# directorysearcher
$ds = New-Object System.DirectoryServices.DirectorySearcher($de,"(objectclass=user)")
$ds.PageSize=1000
# invoke
$ds.FindAll() | %{"do stuff with $_"}
$thisuser = $ds.FindOne()
@ScriptingPro
ScriptingPro / FQDN_HostName.ps1
Created March 29, 2018 22:06
Get FQDN DNS HostName of current Computer
[System.Net.Dns]::GetHostByName([System.Environment]::MachineName).HostName
@ScriptingPro
ScriptingPro / Misc .NET Classes and Methods
Created April 5, 2018 14:12
Use Misc .NET Classes and Methods from PowerShell
[System.Net.Dns]::GetHostByName([System.Environment]::MachineName).HostName
@ScriptingPro
ScriptingPro / find_it.ps1
Last active September 18, 2020 13:01
Search your PS1 files and open in ISE
# Find PowerShell Code Snippets Function
function findit
{
param
(
[Parameter(Mandatory=$True,Position=0)][string]$SearchString,
[Parameter(Mandatory=$False)]$Path = "$env:USERPROFILE\Documents",
[Parameter(Mandatory=$False)]$Filter = "*.ps1"
)
Get-ChildItem -Path $Path -Filter $Filter -Recurse | Select-String $SearchString | select path, @{n="MatchingLines";e={"$($_.LineNumber.tostring("000")): $($_.Line -replace "^[ \t]*",'')"}} | group path | select name, @{n="Matches";e={$_.Group.MatchingLines | Out-String}} | Out-GridView -PassThru | %{psedit -filenames $_.name}
@ScriptingPro
ScriptingPro / Net framework versions installed.ps1
Created August 14, 2018 19:35
Determine .NET versions installed powershell
Get-ChildItem 'HKLM:\SOFTWARE\Microsoft\NET Framework Setup\NDP' -recurse |
Get-ItemProperty -name Version,Release -EA 0 |
Where { $_.PSChildName -match '^(?!S)\p{L}'} |
Select PSChildName, Version, Release
@ScriptingPro
ScriptingPro / AD Object Property Types and New-AD* Cmdlet Parameter Types.ps1
Created September 5, 2018 14:20
AD Object Property Types and New-AD* Cmdlet Parameter Types
# Determine which AD Group attributes are of type string
$GetGroupStringProperties = $((Get-ADGroup -Filter * -ResultSetSize 1 -Properties *).psobject.properties | ?{$_.TypeNameOfValue -eq "System.String"}).Name
# Determine which parameters are expecting strings for New-AdGroup
$NewGroupStringParameters = $((Get-Help New-ADGroup).parameters.parameter | ?{$_.type.name -eq "string"}).Name
$thisguid = 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx'
$guidobj = New-Object System.Guid
[guid]::TryParse($thisguid,[ref]$guidobj)