Welcome back!
Today, I want to provide a small snippet that could list all the monitors installed in you system.
This was quite quick and easy.
Thanks!
Welcome back!
Today, I want to provide a small snippet that could list all the monitors installed in you system.
This was quite quick and easy.
Thanks!
Below is small snippet that gives you info about all the network cards installed on your system.
As you know, $adapter used above can be used to get more and more details you want.
Hope this was quick and easy.
Thanks!
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:
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!
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.
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 !!!
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...