PowerShell
- 13 March
Wait for Command to Finish in PowerShell
Using Start-Process Cmdlet Use Start-Process with the -Wait parameter to wait for the command to finish. [crayon-67bb0b8c849ff048180381/] OUTPUT: In the above output, we can see that the command waits until the file explorer is opened. To do that, we used Start-Process with the -Wait parameter. The Start-Process cmdlet is used to start one or multiple […]
- 13 March
Check if Array Contains Element in PowerShell
1. Overview In this article, we will see different ways to check if array contains element in PowerShell using -contains operator, Contains() method, Where-Object cmdlet, -in operator, indexOf() method, and LINQ. 2. Introduction to Problem Statement We are given an array and element. Our goal is to check if array contains the element. For instance, […]
- 13 March
Download File from URL in PowerShell
Using Invoke-WebRequest Cmdlet Use the Invoke-WebRequest cmdlet to download the specified file from the specified URL to the local path in your machine. [crayon-67bb0b8c84f83964666672/] First, we created two variables named $URL and $LocalPath containing a URL and the local path of your machine. Note that variables are prefixed with the $ sign in PowerShell. Next, […]
- 11 March
Delete Files Older than X Days in PowerShell
Delete Files Older than X Days in PowerShell Use Remove-Item cmdlet with the Get-ChildItem cmdlet and Where-Object cmdlet to delete files older than X days. [crayon-67bb0b8c85111228169590/] The above script retrieved all the items from the given path, filtered them based on the specified property and kept only those files/folders that were less than the given […]
- 10 March
Ping List of Computers in PowerShell
Different Ways to Ping a List of Computers in PowerShell There are multiple ways that we can use to ping a list of computers in PowerShell. Now, what does ping means? Pinging a computer means determining if the networked computer is available and responsive. If the networked computer (we can also call it a target […]
- 10 March
PowerShell Array to String
Using Double Quotation Marks Use double quotation marks to convert array object to string in PowerShell. [crayon-67bb0b8c853f0204700804/] [crayon-67bb0b8c853f4891025540/] First, we created an array object and saved it in the $arrayObject variable; we can also create an array object using the array operator (@()), for instance, $arrayObject = @("How", "are", "you?"). Next, we enclosed the $arrayObject […]
- 08 March
Return Value from Function in PowerShell
Using the return Keyword PowerShell allows us to use the return keyword to return single or multiple values from a function. Let’s learn both of the scenarios. Use the return Keyword to Return a Single Value To return a single value from a particular function: Create a function that returns something, a string, a number […]
- 08 March
Check if String is Null or Empty in PowerShell
Using -not and -eq Operators Use the -not operator to check if the string is null or empty in PowerShell. [crayon-67bb0b8c8574f926737909/] [crayon-67bb0b8c85751875662112/] First, we declared the $string variable and initialized it with $null. What is $null? It is an automatic variable in PowerShell representing the NULL value. Now, what is NULL? We can think of […]
- 08 March
Convert String to Int in PowerShell
Using Dynamic Casting Use dynamic casting to convert the given string to int in PowerShell. [crayon-67bb0b8c858d6903437779/] [crayon-67bb0b8c858d9198681593/] First, we created and initialized a $string variable; then, we specified the type as [int] before the string variable to do dynamic casting. In dynamic casting, we tell the compiler to think about things differently. To cross-check, we […]