Found 989 Articles for Software & Coding

How to use Hashtable splatting in PowerShell?

Chirag Nagrekar
Updated on 18-Jan-2021 07:21:18

663 Views

Splatting is the way to pass the collection of the parameters to the command as a single value. It uses the Hashtable splatting means that we can pass the Name and Value pair combination. We can use the named positional parameter for this with the values we want to provide.For example, First, we will check how we run the Copy-Item command here without splatting, $params = @{    Path = 'C:\Temp\25Aug2020.txt'    Destination = 'C:\test1'    Verbose = $true    Force = $true } Copy-Item @paramsAnother Example, $hash = @{    From = 'harris@Microsoftmail.com'    To = 'Jacob@MicrosoftMail.com'    SMTP ... Read More

What is splatting in PowerShell?

Chirag Nagrekar
Updated on 18-Jan-2021 07:20:14

268 Views

PowerShell splatting is a method to pass the collection of the parameters as a single command unit which makes the command shorter and easier for the user to read commands. Splatting uses the symbol (@) instead of ($) which tells the user that splatting is used and PowerShell is passing a set of values instead of a single value.Splatting in PowerShell was included from the v3.0 onwards and you can pass all parameters in the command.For example, $params = @{    Path = 'C:\Temp\25Aug2020.txt'    Destination = 'C:\test1'    Verbose = $true    Force = $true } Copy-Item @paramsThe splatting ... Read More

How to change Azure Subscription in PowerShell?

Chirag Nagrekar
Updated on 03-Nov-2023 03:24:32

29K+ Views

To change the azure subscription using PowerShell, we can use the Select-AZSubscription command. When you use this command, you can use either the subscription ID, Subscription Name, or the Tenant ID.ExampleWith Subscription Name,Select-AzSubscription -SubscriptionName 'Visual Studio'With TenantID,Select-AzSubscription -Tenant 'XXXX-XXXXX-XXXXXXX-XXXX'With Subscription ID,Select-AzSubscription -SubscriptionId 'XXXX-XXXXX-XXXXXXX-XXXX'Sometimes on console messages will appear that one or more subscriptions are active. In that case, you can switch the other subscription using the Set-AZContext command and you can use subscription ID or the Name for it.ExampleSet-AzContext -SubscriptionId "xxxx-xxxx-xxxx-xxxx" OrSet-AzContext -SubscriptionName "Visual Studio"

How to use PSCustomObject in PowerShell foreach parallel loop?

Chirag Nagrekar
Updated on 04-Jan-2021 10:03:29

3K+ Views

To use the PSCustomObject inside the Foreach Parallel loop, we first need to consider how we are using the variables inside the loop.$Out = "PowerShell" ForEach-Object -Parallel{    Write-Output "Hello.... $($using:Out)" }So let see if we can store or change a value in the $out variable.Example$Out = @() ForEach-Object -Parallel{    $using:out = "Azure"    Write-Output "Hello....$($using:out) " }OutputLine |    4 | $using:out = "Azure"      | ~~~~~~~~~~      | The assignment expression is not valid. The input to an assignment operator must be an object that is able to accept      | assignments, such as a ... Read More

How to work with Invoke-Command Scriptblock output?

Chirag Nagrekar
Updated on 04-Jan-2021 10:01:18

7K+ Views

When we simply write the Invoke-Command, it shows the output on the console.ExampleInvoke-Command -ComputerName Test1-Win2k12 -ScriptBlock {Get-Service}OutputIt shows the output along with the computer name.Now let's say you want to sort the output or you want to work with the output you need to store it. It is similar like we store the output in the variable but we can’t store the output inside the scriptblock and display it outside.$ser = @() Invoke-Command -ComputerName Test1-Win2k12 -ScriptBlock {$ser = Get-Service} Write-Output "Services output" $serYou won’t get any output of the above command because Invoke-Command is known to work on the remote computer. ... Read More

How to check which Azure account is logged in using PowerShell?

Chirag Nagrekar
Updated on 04-Jan-2021 09:58:54

3K+ Views

To check the logged-in Azure user account in the console using PowerShell, you can check the context of the Azure and for that Get-AZContext command is used.ExampleGet-AzContextOutputIf you are already logged in with multiple user accounts then there may be chances that there are multiple contexts available, to list all the available context, use the below command,ExampleGet-AzContext -ListAvailableOutputYou can choose the context using the Select-AZContext command.

How to use a variable inside a Foreach-Object Parallel?

Chirag Nagrekar
Updated on 04-Jan-2021 09:57:13

3K+ Views

There are two different types of the variable we can use inside foreach parallel loop. One that is declared inside and the other that is declared outside of the foreach parallel loop.Please note − We are discussing here Foreach-Object Parallel loop, featured in PowerShell version 7. For a normal foreach loop inside and outside variables are the same.Variable declared inside the Foreach parallel loop can be used directly with its name. For example, Example$vms = "TestVm1", "TestVM2", "TestVm3" $Vms | ForEach-Object -Parallel{    $var1 = $_    Write-Output "Testing VM: $var1" }OutputTesting VM: TestVm1 Testing VM: TestVM2 Testing VM: TestVm3In ... Read More

How to use Wait-Process in Powershell?

Chirag Nagrekar
Updated on 04-Jan-2021 09:55:46

1K+ Views

Wait-Process cmdlet in PowerShell is used to wait for the process to stop before the execution moves on to the next step.ExampleWe have a snipping tool application running and we need to wait for the process to stop first and then move on to the next step.PS C:\> Get-Process SnippingTool | Select Name, Id, CPU Name          Id    CPU ----          --    --- SnippingTool  7440   2.0625To wait for the process to stop first we will use the Wait-Process command. You can provide ProcessName or ID.Write-Output "Waiting for the Process to Stop" ... Read More

How to use the Timeout command in PowerShell?

Chirag Nagrekar
Updated on 04-Jan-2021 09:53:14

16K+ Views

Timeout.exe is actually a cmd command which can also be used in PowerShell. Let see the help related to Timeout command.timeout /?If we see the timeout parameters list we can use /T which indicates the Time in seconds and /NoBreak command, which Ignores any key for the specified time.ExampleWrite-Output "Timeout is for 10 seconds" Timeout /T 10 Write-Output "This line will be executed after 10 seconds if not interuptted"OutputPS C:\> C:\Temp\TestPS1.ps1 Timeout is for 10 seconds Waiting for 5 seconds, press a key to continue ...Please note: In the above example, the user can interrupt the timeout seconds using any key to disallow ... Read More

How to send email using PowerShell?

Chirag Nagrekar
Updated on 04-Jan-2021 09:51:23

3K+ Views

To send email using PowerShell, there are multiple methods but there is a simple command called SendMailMessage. This command is a part of the module called Microsoft.PowerShell.UtilityTo send email using the specific SMTP server we need to add the SMTP server parameter.Send-MailMessage `    -From 'User1@TestDomain.com' `    -To 'User2@TestDomain.com' `    -Subject 'Test Email' `    -SmtpServer 'Smtp.TestDomain.com'In the above example, an email will be sent from the -From parameter, a user to -To parameter users with the subject name ‘Test Email’ with the specified SMTP server name.If you have multiple users then you can separate them using a ... Read More

Advertisements