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-679097a6a9805245765813/] 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-679097a6abb00266892814/] 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-679097a6abc87782331525/] 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-679097a6abf56428157894/] [crayon-679097a6abf58773896163/] 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-679097a6ac2b1889749549/] [crayon-679097a6ac2b4850350027/] 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-679097a6ac41d026870380/] [crayon-679097a6ac421397360086/] 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 […]