Found 461 Questions for PowerShell

How to use the foreach loop parallelly in PowerShell?

Chirag Nagrekar
Updated on 19-Feb-2021 13:05:09
There are two ways to use the foreach loop parallelly in PowerShell.Using Foreach-Object -Parallel command (Supports in PowerShell 7.0 or above)Using Foreach -Parallel in Workflow (Supports PowerShell 5.1 or below)Suppose we have Servers.txt and which contains 10 Servers. When we use the Parallel for loop, it isn’t guaranteed which server loop will pick first as shown below with two examples.Using Foreach-Object-Parallel command. (not Foreach -Parallel)This Foreach-Object -Parallel command feature is newly added to the PowerShell version 7.0 or above.Example$servers = Get-Content C:\Temp\Servers.txt $servers | foreach-Object -parallel{    Write-output "Working on $_" }OutputPS C:\> C:\Temp\Test1.ps1 Working on IndiaServer003 Working on IndiaServer002 Working on IndiaServer001 Working on ... Read More

What is the PowerShell Workflow?

Chirag Nagrekar
Updated on 19-Feb-2021 13:01:31
PowerShell workflow is built on the .Net based Windows Workflow Foundation (WWF) and it has a separate Workflow engine to execute the code because it translates code into XAML for the WWF framework.PowerShell workflow is series of steps that is mainly used forRunning activities parallelly on more than one machine.Long-running scripts.Structuring steps (Which steps will execute parallel and which in sequence)Frequently used tasks.Resuming the script from where it was terminated due to system failure or any other interruption by defining checkpoints.The PowerShell workflow was introduced in PowerShell 3.0 and discontinued for windows in the Core version (6.0 onwards) and only ... Read More

How to validate the IP address using PowerShell?

Chirag Nagrekar
Updated on 19-Feb-2021 12:59:09
To validate the IP address using PowerShell, we can use several methods as shown below.Using RegEx method.Using Typecasting method.Using TypeCasting method.In this method, we are using System.Net method class IPAddress to validate the IP address.[ipaddress]$IP = "192.168.0.1"The above one is valid. Let’s check what $IP stores in.PS C:\> $IP Address : 16820416 AddressFamily : InterNetwork ScopeId : IsIPv6Multicast : False IsIPv6LinkLocal : False IsIPv6SiteLocal : False IsIPv6Teredo : False IsIPv4MappedToIPv6 : False IPAddressToString : 192.168.0.1The above one is Ipv4 and this method also works for IPv6. See the example below.PS C:\> [ipaddress]"2001:0db8:85a3:0000:0000:8a2e:0370:7334" Address : AddressFamily : InterNetworkV6 ScopeId ... Read More

How to check if the string contains the specific word?

Chirag Nagrekar
Updated on 08-Feb-2021 07:55:01
To check if the PowerShell string contains a specific word, we can use the string method Contains(). For example, ExamplePS C:\> $str = 'TestNZ01LT' PS C:\> $str.Contains('NZ') TrueNow the funny thing is, even if the PowerShell is case-Insensitive, the above command is not. We need to provide the exact substring. For example, the below output will be false.ExamplePS C:\> $str.Contains('Nz') FalseTo overcome this problem, we can either provide the same search name in the method or we need to use the lowercase or uppercase method if you don’t want the search case-sensitive.PS C:\> $str = 'TestNZ01LT' PS C:\> ($str.ToLower()).Contains(('Nz').ToLower()) True ... Read More

How to convert command output to the Hashtable format in PowerShell?

Chirag Nagrekar
Updated on 08-Feb-2021 07:53:26
To convert any command output to the hashtable in PowerShell, we can first convert the output to the JSON format using the ConvertTo-JSON command, and applying the ConvertFrom-JSON command from the first output will produce an output to the Hashtable.ExampleGet-ServiceThe above command will give the output in the array format.OutputStatus  Name            DisplayName ------  ----            ----------- Stopped WwanSvc         WWAN AutoConfig Stopped XblAuthManager  Xbox Live Auth Manager Stopped XblGameSave     Xbox Live Game Save Stopped XboxGipSvc      Xbox Accessory Management Service Stopped XboxNetApiSvc   Xbox Live ... Read More

How to exclude the RunSpaceID property from the Invoke-Command output in PowerShell?

Chirag Nagrekar
Updated on 08-Feb-2021 07:51:36
When we write Invoke-Command in PowerShell, sometimes we get the RunSpaceID property in PowerShell. This is because we use Select-Object (alias: Select) command inside the scriptblock. For example,ExampleInvoke-Command -ComputerName LabMachine2k12 -ScriptBlock{Get-Process PowerShell | Select Name, CPU, WorkingSet}OutputName           : powershell CPU            : 9.1572587 WorkingSet     : 127700992 PSComputerName : LabMachine2k12 RunspaceId     : c690f205-06d4-4cc4-be29-5302725eadf1To avoid getting the RunSpaceID property in the output, use the Select command output the scriptblock. For example,ExampleInvoke-Command -ComputerName LabMachine2k12 -ScriptBlock{Get-Process PowerShell} | Select Name, CPU, WorkingSetOutputName       CPU        WorkingSet ----       ---        ---------- powershell 9.1572587  127700992

How to exclude PSComputerName property from Invoke-Command output in PowerShell?

Chirag Nagrekar
Updated on 08-Feb-2021 07:49:29
When we are working with Invoke-Command, we get the PSComputerName extra field in the output table. You can remove it by using -HideComputerName parameter. For example, the Below command gives the output with the PSComputerName property.ExampleInvoke-Command -ComputerName LabMachine2k12 -ScriptBlock{Get-Service Winrm} | ft -AutoSizeOutputStatus  Name  DisplayName PSComputerName ------  ----  -----------     -------------- Running Winrm Windows Remote Management (WS-Management) LabMachine2k12To hide the PSComputerName property,ExampleInvoke-Command -ComputerName LabMachine2k12 -ScriptBlock{Get-Service Winrm} -HideComputerNameOutputStatus  Name  DisplayName   ------  ----  -----------   Running Winrm Windows Remote Management (WS-Management)

How to find the device driver version using PowerShell?

Chirag Nagrekar
Updated on 08-Feb-2021 07:46:08
To find the device driver version using PowerShell, we need to use the class win32_PnpSignedDriver of the WMI object. For example, ExampleGet-WmiObject win32_PnpSignedDriverOr if you are using PowerShell core (PowerShell 6.0 or later), you can use the CIM Instance command. For example, Get-CimInstance win32_PnpSignedDriverTo filter out the Drivers against their versions, use the below command to filter.Examplegwmi win32_PnpSignedDriver | Select Description, DriverVersionOutputACPI x64-based PC                6.2.9200.16384     UMBus Root Bus Enumerator        6.2.9200.16384     WAN Miniport (IPv6)              6.2.9200.16384     Composite Bus Enumerator ... Read More

How to check windows certificate expiry date using PowerShell?

Chirag Nagrekar
Updated on 08-Feb-2021 07:44:24
To get the particular windows certificate expiry date from the particular store, we first need the full path of that certificate along with a thumbprint. If the thumbprint is not known to you, we can use the friendly name.With the thumbprint, Get-ChildItem Cert:\LocalMachine\root\0563B8630D62D75 | fl *When you run the above command, it will get all the details of the certificate having thumbprint 0563B8630D62D75.There you can see there are two fields listed, NotAfter and NotBefore which shows the expiry and start dates respectively. To filter them out, ExampleGet-ChildItem Cert:\LocalMachine\root\0563B8630D62D75 | Select FriendlyName, NotAfter, NotBeforeOutputFriendlyName NotAfter   NotBefore ------------ --------   --------- ... Read More

How to get the Windows certificate details using PowerShell?

Chirag Nagrekar
Updated on 08-Feb-2021 07:42:37
We know that the Windows Certificates are resided in the Certificate store but finding the certificate with its name or getting particular certificate details might be cumbersome sometimes.You can access the certificate store using MMC or using CertMgr.msc command. There are certificates stored for CurrentUser, ServiceAccount, and Local Computer. To access the certificate store using PowerShell, you need to access the PSDrive, and Certificates are stored in the drive called Cert as you can see below.PS C:\> Get-PSDrive cert | ft -AutoSize Name Used (GB) Free (GB) Provider Root CurrentLocation ---- --------- --------- -------- ---- --------------- Cert Certificate \Let say ... Read More
Advertisements