Skip to main content


Showing posts with the label PowerShell

PowerShell Match Drive Letter to Volume ID in AWS

Matching AWS volume ID to its corresponding Windows drive letter is an easy task using PowerShell.
Win32_DiskDrive holds the info for the volume-ID.
Here’s the script, below enjoy 😊

$get_disks=gwmi-query"SELECT * FROM Win32_DiskDrive"
ForEach($diskin$get_disks){ $get_partitions=gwmi-query"ASSOCIATORS OF {Win32_DiskDrive.DeviceID='$($disk.DeviceID)'} WHERE AssocClass = Win32_DiskDriveToDiskPartition"
$get_logicaldisks=gwmi-query"ASSOCIATORS OF {Win32_DiskPartition.DeviceID='$($partition.DeviceID)'} WHERE AssocClass = Win32_LogicalDiskToPartition" ForEach($logicaldiskin$get_logicaldisks){
$insert_the_dash= ($disk.SerialNumber).Insert(3,"-") #insert dash to match AWS volume
$volume_partition="Drive Letter "+$logicaldisk.DeviceID +"is equals to volume: "+$insert_the_dash
Write-Output$volume_partition } }

PowerShell check Windows boot time and uptime

Checking boot time is essential in determining the server or workstation uptime.
If ever there is a server or computer that is not supposed to be rebooted or shutdown but then to check whether the server or computer hasn’t done a power cycle is to check its uptime.
Checking the uptime will determine how long the server has been online.
PowerShell is just an awesome tool to do this kind of task.

Here’s the script:
#run a command line using PowerShell $boot_time=&systeminfo|find"System Boot Time" Write-Output$boot_time
#display a pop-up message box using PowerShell [System.Windows.MessageBox]::Show('Hello, the '+$boot_time)
#split string using regex and match spaces $splitx=$boot_time-split'\s+'-match'\S'
#get the 4th array on the array element $xtime=Get-Date$splitx[4]
#get the current date and time of the system $Date=Get-Date
#subtract the current date and time with the boot time $get_duration=$Date-$xtime $xmin_total=$get_duration.TotalMinut…

PowerShell Add Custom Result Property

PowerShell has its own or default format of displaying the results or the output of the cmdlets or PowerShell commands.
However, PowerShell is customisable, and the output property can be modified.
Example below, gets the name of the file and display the date only without the time.
PowerShell code snippet below filter a single  and gets its lastwrite date.

get-childitem "C:\Users\ps_testing\Documents" -filter IMXP2611.jpg -recurse |select Directory, Name, @{Name="LastWriteDate";   Expression={$_.LastWriteTime.ToString("yyyy-MM-dd")}} | ft -wrap

This can be modified using wildchars like "*", which filters all "jpg" regardless of filename.

get-childitem "C:\Users\ps_testing\Documents" -filter *.jpg -recurse |select Directory, Name, @{Name="LastWriteDate";   Expression={$_.LastWriteTime.ToString("yyyy-MM-dd")}} | ft -wrap

LastWriteDate == is a custom label for the output
yyyy-MM-dd == is a custom format that dis…

PowerShell regex count specific character or number

Example input is: 0009911000

$Input_Number = '0009911000'

#[1] = tells regex to match on the right or look forward
#[0] = tells regex to match on the left or look backward

#Matches($Number,'0+') means #Matches($input, $pattern)

Write-Output $xtotal Write-Output $ytotal
Image output from PowerShell ISE:

Above code works or matches if input is something like this: “0009911000” There are zeroes on the left and right and in between are any numbers or characters.
For input like this: 0000991100010 in which zeroes are in different position of the string the below code will work.
$Input_Number = '0000991100010' $xytotal=[regex]::Matches($Input_Number,'[0]*[0]*[0]+') Write-Output $xytotal

Output will be something like this: Groups: {0000} Success: True Captures : {0000} Index: 0 Length: 4 Value: 0000
Groups: {000} Success: T…

Test AD account password without logging in to the computer

Documenting AD password is a good practice and should be a part of IT policy.
It’s easier to troubleshoot and support users if the password is known to IT. Of course, ethical use of the password should be followed.
How to test an active directory account password whether it’s working or not, without logging in or signing in on a computer?
PowerShell can help to test a user account password quite easily.
AD admin account is needed and the PowerShell must be elevated while running the script below. Correct version of PowerShell is needed for this script.
See image below:

That’s it! Just replace the username and password to be tested and you can tell your users that the password is not working and needs password reset for their account to work.
The script is written for educational purposes and used it ethically.
Cheers!! Till next time. Happy Coding!!!
================================ Free Android Apps:
Click  links below to find out more:
Excel Keyboard guide:…

Error: the type or namespace name does not exist in the namespace using visual studio 2017

When compiling an error will occur that a name space does not exist. An example error like the message below:
The type or namespace name 'Microsoft.Office.Interop.Outlook' does not exist in the namespace 'Microsoft'(are you missing an assembly reference?)
The system is trying to find a reference for a specific namespace or dll.
One solution for this is to add the reference to the missing namespace.
Find the “solution explorer” window like the image below:

After opening the “solution explorer” window, right click the name of the project just below the solution explorer. Beside the C# icon.
A new window will open like the image below. Click “Add”, then click “Reference”.

After clicking “Reference”, the “Reference Manager” window will open.
Click “COM” option, then use the filter option to go directly to the desired namespace or reference. Since the error is a Microsoft namespace, type “Microsoft” and “Microsoft” namespace will be filtered from the list. See image below…

PowerShell Insert Multiple Lines or text to another file dynamically

Copying and pasting text to another file is a common thing to do, copy and paste done. Boom!
In PowerShell scripting copying and pasting text from one file to another dynamically isn’t as easy as it is done in a spreadsheet or document. Dynamically while the code is running  and doing things in the background without human intervention.
As the odds say, if there’s a will there’s a way. And making things efficient and easy is a good way in life.
If ever a scenario occurs that you need to copy text or lines in a file from one file to another, without breaking the code execution. Script below works just fine. Just insert the code where it is needed.

PowerShell code snippet:
$reader=[System.IO.File]::OpenText("D:\test-read-paste\main1.txt") #get-content can also be used
$lineNumberx=3 #adjust this number where you need to start inserting the text
#Loop line by line and process while($null-ne ($line=$reader.ReadLine())) {