Personal Website, Tech Blog

Category: PowerShell

How to quickly resolve multiple PTR records in Powershell

Sometimes there is a need to fetch hostname records for multiple ip addresses. In my case I had a file with wrong hostnames. I had to quickly collect the proper hostnames to use them in another script.

$dir = "C:\Desktop\scripts\"
$report = @()
$ipaddress=Get-Content -Path $dir\iplist.txt
foreach ($ip in $ipaddress) {
    $response = [System.Net.Dns]::GetHostEntry("$ip")
    $HostName = $response.HostName
    $report += [pscustomobject]@{
        HostName = $HostName
        IpAddress = $ip

How to overwrite local files with files from a GIT repository

PS C:\proj> git fetch --all

Fetching origin
remote: Counting objects: 74, done.
remote: Compressing objects: 100% (74/74), done.
Unpacking objects:  40% (30/74)eused 0 (delta 0)
Unpacking objects: 100% (74/74), done.
   cbd7b35..852c958  master     -> origin/master
PS C:\proj> git reset --hard origin/master

HEAD is now at 852c958 minor changes

How to generate large files with random content in PowerShell

Sometimes there is a need to test bandwidth of Windows network or disk subsystem. Usually it’s a hassle to find and transfer large enough incompressable files onto the server to perform an accurate speed test.

The following PowerShell code creates multiple 1GB files with random content. I my case the files were barely compressible, ideal for testing WAN link performance without worrying that WAN Optimizers will affect the test result.

To change number of generated files adjust the “-le 100” part.

for ($i=1; $i -le 100; $i++)
    $out = new-object byte[] 1073741824; (new-object Random).NextBytes($out);           [IO.File]::WriteAllBytes("c:\file$i.txt", $out)

Useful Active Directory Commands (PowerShell)

There are couple of account-related PowerShell commands that I use on a frequent basis. They are helpful in access troubleshooting, to check that user accounts and groups configuration is correct.

(Windows 10) Before using them you will need to install Remote Server Administration Tools and enable “activedirectory” Powershell module (you can find more details here).

Get information about “jdoe” user (e.g. if user account is locked, home drive location, etc.)

Get-ADUser -Identity jdoe -Properties * -server ldapserver.local

Surname           : Doe
Name              : Jon Doe
UserPrincipalName : 
GivenName         : John
Enabled           : TRUE
SamAccountName    : jdoe 
ObjectClass       : user
SID               : S-1-5-21-2889043008-4136710315-25555524263-3544
ObjectGUID        : e14177764-096c-4cb0-b903-ebb66562d99d

Get information about “Administrators” group

Get-ADGroup Administrators -server ldapserver.local

DistinguishedName : CN=Administrators,CN=Builtin,DC=ldapserver,DC=com
GroupCategory     : Security
GroupScope        : DomainLocal
Name              : Administrators
ObjectClass       : group
ObjectGUID        : 02ce3444-dd86-41ba-bddc-013f34432178
SamAccountName    : Administrators
SID               : S-1-5-32-444

Get group memberships of “jdoe” user

Get-ADPrincipalGroupMembership jdoe -server ldapserver.local|select name


Get members of “Administrators” group

Get-ADGroupMember -identity Administrators  -server ldapserver.local|select name


Change password of “jdoe” user

Set-ADAccountPassword -Identity jdoe -server ldapserver.local

Please enter the current password for 'CN=jdoe CN=Pat,CN=Users,DC=ldapserver,DC=local'
Please enter the desired password for 'CN=jdoe CN=Pat,CN=Users,DC=ldapserver,DC=local'
Repeat Password:***********

Remove Active Directory object (e.g. SVM, sometime helpful when there is a problem joining SVM to the domain)

Remove-ADComputer -Identity SVM1 -Server ldapserver.local -Credential (Get-Credential)

cmdlet Get-Credential at command pipeline position 1
Supply values for the following parameters:
User: LOCAL\LDAP_user
Password for user LOCAL\LDAP_user: ****************
Are you sure you want to perform this action?
Performing the operation "Remove" on target "CN=SVM1,OU=STORAGE,DC=ldap,DC=local".
[Y] Yes [A] Yes to All [N] No [L] No to All [S] Suspend [?] Help (default is "Yes"):

© 2021 Jakub Bielaszka

Theme by Anders NorenUp ↑

Social media & sharing icons powered by UltimatelySocial