Tuesday, February 13, 2024

#105: Get list of all the monitors installed in your system using Powershell

Welcome back!

Today, I want to provide a small snippet that could list all the monitors installed in you system.

# Get all monitors
$monitors = Get-WmiObject -Namespace root\cimv2 -Class Win32_DesktopMonitor


# Output information about each monitor
foreach ($monitor in $monitors) {
   
    Write-Output "Monitor Name: $($monitor.Name)"
    Write-Output "Monitor Manufacturer: $($monitor.Manufacturer)"
    Write-Output "Monitor Screen Height: $($monitor.ScreenHeight)"
    Write-Output "Monitor Screen Width: $($monitor.ScreenWidth)"
    Write-Output "-----------------------------"
}


This was quite quick and easy.

Thanks!

#104: Get all the network cards installed in system using PowerShell

Below is small snippet that gives you info about all the network cards installed on your system. 


# Get all network adapters
$networkAdapters = Get-NetAdapter

# Output information about each network adapter
foreach ($adapter in $networkAdapters) {
    Write-Output "Name: $($adapter.Name)"
    Write-Output "Description: $($adapter.Description)"
    Write-Output "Interface Index: $($adapter.InterfaceIndex)"
    Write-Output "MAC Address: $($adapter.MacAddress)"
    Write-Output "Status: $($adapter.Status)"
    Write-Output "-----------------------------"
}

As you know, $adapter used above can be used to get more and more details you want. 

Hope this was quick and easy. 

Thanks!

#103: Get all the sound cards installed on system

 Sometimes, we get into situation where we are developiong something that requires listing all the soundcards in the system. You can do it easily with below code: 


# Get all sound devices
$soundDevices = Get-PnpDevice -Class AudioEndpoint

# Output information about each sound device
foreach ($device in $soundDevices) {
    Write-Output "Device ID: $($device.DeviceID)"
    Write-Output "Description: $($device.Description)"
    Write-Output "Manufacturer: $($device.Manufacturer)"
    Write-Output "Driver Version: $($device.DriverVersion)"
    Write-Output "Status: $($device.Status)"
    Write-Output "-----------------------------"
}
w code:        

#102: Hash Tables in Powershell

In PowerShell, a hash table, also known as an associative array or dictionary in other programming languages, is a collection of key-value pairs. Hash tables allow you to store and retrieve data efficiently based on keys rather than numerical indexes. Here's a detailed guide on how to use hash tables in PowerShell:

Creating a Hash Table:

You can create a hash table using the @{} syntax:

Example: 

$hashTable = @{

    Key1 = "Value1"

    Key2 = "Value2"

    Key3 = "Value3"

}

Accessing Values:

You can access values in a hash table by specifying the key:

Example:

$value = $hashTable["Key1"]

Adding or Modifying Values:


You can add or modify values in a hash table by assigning a value to a key:

$hashTable["Key4"] = "Value4"

$hashTable["Key2"] = "NewValue2"


Removing Values:

You can remove a key-value pair from a hash table using the Remove() method:

$hashTable.Remove("Key3")

Checking if a Key Exists:


You can check if a key exists in a hash table using the ContainsKey() method:

if ($hashTable.ContainsKey("Key1")) {

    Write-Output "Key1 exists in the hash table."

}

Iterating Over a Hash Table:

You can iterate over a hash table using a foreach loop:

foreach ($key in $hashTable.Keys) {

    $value = $hashTable[$key]

    Write-Output "$key: $value"

}

Using Hash Tables as Parameters:

You can use hash tables to pass named parameters to functions or cmdlets:

function Test-Function {

    param (

        [string]$Name,

        [int]$Age

    )

    Write-Output "Name: $Name, Age: $Age"

}

$params = @{

    Name = "John"

    Age = 30

}


Test-Function @params


Nested Hash Tables:

You can have nested hash tables to represent more complex data structures:

$nestedHashTable = @{

    Key1 = @{

        NestedKey1 = "Value1"

        NestedKey2 = "Value2"

    }

    Key2 = @{

        NestedKey3 = "Value3"

        NestedKey4 = "Value4"

    }

}

Hash tables are versatile data structures in PowerShell and are commonly used for configuration settings, organizing data, passing parameters, and more. Understanding how to work with hash tables effectively is essential for PowerShell scripting.

Hope you enjoyed this article. 

Thanks!

#101 : Variables in Powershell

 In PowerShell, variables are used to store data that can be referenced and manipulated throughout your script. 

Variable Declaration in Powershell:

Variables in PowerShell do not require explicit declaration of data types. You can simply assign a value to a variable using the $ symbol followed by the variable name.

$myVariable = "Hello, World!"

Variable Naming Convention:
PowerShell variable names are not case-sensitive, but it's a good practice to use camelCase or PascalCase for readability.

Variable Data Types:
PowerShell variables can hold various types of data, including strings, numbers, arrays, and objects.

Accessing Variables:
You can access the value of a variable by simply referencing its name preceded by the $ symbol.

Example: 

Write-Output $myVariable

What is scope of variable?
PowerShell variables can have different scopes, such as
1. Script scope
2. Global scope
3. Function scope
4. Local scope.

The default scope for variables is the script scope. You can specify the scope using the appropriate scope modifier ($script:, $global:, $local:, etc.).

Example:

$globalVariable = "I'm a global variable"

function Test-Scope {

    $localVariable = "I'm a local variable"

    Write-Output $globalVariable

    Write-Output $localVariable

}

Test-Scope


Variable Expansion:

PowerShell supports variable expansion within strings enclosed in double quotes, allowing you to include variable values directly within a string.

Example:

$name = "John"
Write-Output "Hello, $name!"

Automatic Variables:
PowerShell also provides a set of automatic variables that store various system and environment information, such as $PSVersionTable, $PID, $HOME, etc.

Variable Manipulation:
You can manipulate variables using various operators and methods, such as assignment (=), addition (+=), subtraction (-=), multiplication (*=), division (/=), and so on.

$num = 5

$num += 10

Write-Output $num  # Output: 15

These are some of the fundamental concepts related to variables in PowerShell. As you become more familiar with PowerShell scripting, you'll discover additional techniques and best practices for working with variables effectively.

Wednesday, September 16, 2020

#100: How to execute a string in Powershell?

 Rarely, but sometimes we need to run the command from an expression that we create. I stumbled upon a situation where I was to read the content of a config file and run it. 

There could be other ways to do it, but invoke-expression can be used with simplicity. 


Invoke-Command

SYNTAX

    Invoke-Command [-ScriptBlock] <scriptblock> [-NoNewScope] [-InputObject <psobject>] [-ArgumentList <Object[]>]

    [<CommonParameters>]


EXAMPLE: 

Read content of a file that has EXPRESSION=get-help ls 

$str=gc "c:\temp\cmd_list.txt" | where { $_ -match "EXPRESSION=" } | foreach { $_.split("=")[1] 

invoke-command $str

      

      

I hope it was quite easy and simple. 

Happy scripting !!!

Saturday, June 13, 2020

#98: How to know WIFI password?


PROBLEM:

It is quite obvious to forget password and keep trying password again and again. It happens with WIFI also and gives a real hard time. 


SOLUTION:

To get WIFI password, you can try below command, quick and simple. 

(netsh wlan show profiles) | Select-String "\:(.+)$" | %{$name=$_.Matches.Groups[1].Value.Trim(); $_} | %{(netsh wlan show profile name="$name" key=clear)}  | Select-String "Key Content\W+\:(.+)$" | %{$pass=$_.Matches.Groups[1].Value.Trim(); $_} | %{[PSCustomObject]@{ ProfileName=$name;Password=$pass }}


NOTE:
With this method, you get your WIFI profiles password, not your neighbour's ðŸ˜„.

Happy scripting!!!

#112: How to handle xml document in Powershell?

 In PowerShell, you can handle XML data using various cmdlets and methods provided by the .NET Framework. Here's a basic guide on how to...