Found 463 Articles for PowerShell

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

How to retrieve Azure VMs using PowerShell?

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

583 Views

To retrieve the azure VMs using PowerShell, we can use Get-AzVM commands but before that make sure you logged in using Azure Credentials in the console. When you type this command, you will get the list of all VMs in the specified subscription.To check which all properties are supported you can use theGet-AzVM | gm -MemberType PropertiesYou can select different properties from there using the Select-Object command (Alias: Select). To retrieve the VMs from the specific ResourceGroup, use the below command.Get-AzVM -ResourceGroupName AUTOMATIONTESTRG2If your VM is in a different subscription then you need to switch the subscription and need to ... Read More

How to Install Azure PowerShell Cmdlets?

Chirag Nagrekar
Updated on 04-Jan-2021 09:47:50

576 Views

Before Installing the Azure cmdlets for PowerShell, it is recommended to upgrade it to the PowerShell version 7.X to leverage the new features.To install the PowerShell cmdlets for Azure, you need to download and install the AZ module.Install-Module -Name Az -AllowClobber -Scope CurrentUserTo install it for all the users, Install-Module -Name Az -AllowClobber -Scope AllUsersIf the AzureRM module is already installed, you first need to uninstall it because both modules AzureRM and AZ cannot reside in the same console and the AzureRm module is going to decommission soon. So anyway we need to upgrade it to the latest AZ Module.To ... Read More

How to connect Azure Account using PowerShell?

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

3K+ Views

To connect the azure account with PowerShell, we can use the Connect-AZAccount command. If we check the command parameters from the below URL, there are multiple methods we can connect to the azure account but in this article, we will use the simple methods to connect.Using the Interactive console to connect portalUsing DeviceLogin method.Using Credentials method.Using the Interactive console method to connect the portal.When we use the Connect-AZAccount directly without any parameter, it will open a popup for the azure portal credential.You need to enter your Azure credentials there.Using Device Login method.In this method, Connect-AZAccount uses the parameter -DeviceLogin. Once ... Read More

How to check if the computer is connected to a domain using PowerShell?

Chirag Nagrekar
Updated on 28-Dec-2020 07:03:41

13K+ Views

To check if a computer is connected to any domain we can use multiple methods. In this article, we can use two methods. One using the System Information of the computer and the second using the DirectoryServices .Net Class.First method using System Information and filter out a string called “Domain” which shows us if the computer is in the domain or the workgroup.systeminfo | findstr "Domain"OutputIf the computer is in the workgroup, It will show the workgroup name. For example, In the second method, we will use the directory service .Net class method name GetComputerDomain(). If the server is not connected to the ... Read More

Advertisements