PowerShell
- 05 April
PowerShell Split Path into Array
Split Path into Array in PowerShell Using Split() Method Use the Split() method to split the path into an array in PowerShell. [crayon-6790746eba768387460005/] [crayon-6790746eba76e881994994/] First, we defined a $path variable and set its value to a path as demonstrated above; you can initialize it with your own path. Next, we chained the Split() method with […]
- 29 March
Change Directory in PowerShell
Using Set-Location Cmdlet Use the Set-Location cmdlet to change directory in PowerShell. [crayon-6790746ebaaaf779969214/] [crayon-6790746ebaab4274663249/] Above, we have used the Set-Location cmdlet to change the directory from C:\ to D:\ drive. In PowerShell, Set-Location is used to specify the current working directory. First, the working directory was C:\Users\DELL after using Set-Location -Path D:\ current working directory […]
- 29 March
Get Last Element of Array in PowerShell
An array is a collection of data elements stored together under a single variable name. Unlike other programming languages, the array in PowerShell contains the values of the same or different data types. It is defined using square brackets [ ] with each element separated by a comma. The array’s index starts with zero, meaning […]
- 28 March
Get Exit Code of Last Command in PowerShell
We can use different solutions to get the exit code of the last command in PowerShell, but before that, it is essential to know what does exit command means. The exit command terminates the current PowerShell script or session in PowerShell. When an exit command is executed, the PowerShell session or script will be immediately […]
- 19 March
Select-String from File with Regex in PowerShell
Using Select-String with Regex to Find Patterns from File There are multiple scenarios where we can use Select-String with a regular expression (also called regex) to search for particular patterns from file(s). Some of them are given below: Search a specific pattern in a single file Search a particular pattern in multiple files Search multiple […]
- 19 March
Split Text File on NewLine in PowerShell
Using Get-Content Cmdlet We can use multiple ways to split the specified text file into an array based on every new line. To practice those methods and commands, we will use the file.txt file having the following content. Of course, you can also create or use your text file. [crayon-6790746ebb304522387692/] Let’s continue learning the solutions […]
- 19 March
Print Object in PowerShell
Print Object’s Properties in Powershell In PowerShell, an object is a data structure that contains properties and methods. An object is essentially an instance of a .NET class and can be created using the New-Object cmdlet. PowerShell objects are used extensively in PowerShell scripting. For example, many cmdlets return objects as output, which can be […]
- 15 March
PowerShell – Find String in File
1. Overview Searching for strings in text files is a common task in PowerShell, used in scenarios like log file analysis and configuration file searches. This article explores various methods for finding strings in files, including both case-sensitive and case-insensitive approaches. 2. Introduction to Problem Statement Let’s consider a log file named server.log: [crayon-6790746ebb66a044164777/] Our […]
- 13 March
Find Position of Character in String in PowerShell
Using IndexOf() Method Use the IndexOf() to find the position of character in string in PowerShell. indexOf() method returns index of first occurrence of character in String. [crayon-6790746ebb90d519955775/] [crayon-6790746ebb910364857356/] In the above code, the output indicates that the input string is Hello, world! and the position of the comma character in the string is 5. […]