PowerShell
25 JulyMerge Arrays in PowerShell
Using + Operator Use + operator to merge arrays in PowerShell. [crayon-69077e5b9aeff924075819/] [crayon-69077e5b9af09871735260/] We used the sub-expression operator represented by @() to create two separate arrays named $array1 and $array2 (you can also create arrays without using the sub-expression operator). Note that both arrays contained string-type values. Next, we used the + operator to join […]
14 JulyPowerShell Remove Special Characters from String
Using Regex.Replace() Method Use the Regex.Replace() method to remove special characters from the specified string in PowerShell. [crayon-69077e5b9c8da564714164/] [crayon-69077e5b9c8e6996086384/] First, we declared a variable named $str and set its value to a string containing some special characters. Then, we used the Replace() method of the Regex class from the System.Text.RegularExpressions namespace to replace the special […]
14 JulyPowerShell Remove Header from Output
For this article, we will use a CSV file, you can use your own, but we will be using inputFile.csv containing the following data: [crayon-69077e5b9cf2a393943142/] Removing Header from Imported Data We can use various ways to remove the header from the imported data before further processing in PowerShell. Let’s learn them below. Use -HideTableHeaders Parameter […]
29 JunePowerShell Trim String After Character
Using Split()Method Use the Split() method to trim the string after the first occurrence of the specified character in PowerShell. [crayon-69077e5b9d711524305536/] [crayon-69077e5b9d718857295359/] We declared and initialized the $string variable with "Hi! Welcome to Java2Blog! Let's learn." value. Next, we chained the Split() method with the $string variable to split the $string into an array based […]
29 JunePowerShell Find File by Name
Using Get-ChildItem Cmdlet Use the Get-ChildItem cmdlet with -Path and -Filter parameters to find the specified files and directories in the given path in PowerShell. [crayon-69077e5b9e73b919736197/] [crayon-69077e5b9e744582765798/] The Get-ChildItem retrieves items and child items from one or multiple paths (locations). The -Path parameter is used to specify one or multiple paths to locations; we can […]
28 JunePowerShell Add Days to Date
Before we start, knowing how dates are stored in PowerShell is essential. Dates are stored as DateTime, which contains both a date and a time. The dates are in the MM/DD/YYYY format. Let’s continue learning different ways to add days to date in PowerShell. Using AddDays() Method Use the AddDays() method to add days to […]
24 JunePowerShell Add Date to Filename
if you don’t want to rename the file and just want to change in string format, you can directly jump to join-path section. Using Rename-Item Cmdlet Use the -Rename-Item cmdlet to add date to file in PowerShell [crayon-69077e5b9ee57016997905/] [crayon-69077e5b9ee5c544199891/] The above code is functioning in the following manner: It first sets the $Filename variable to […]
24 JunePowerShell Get Last Modified File in Directory
For this article, our working directory will be C:\Test1 containing two files named File1.txt and File2.txt. Using Get-ChildItem along with Sort-Object Cmdlet Use the Get-ChildItem cmdlet with Sort-Object` cmdlet to get last modified file in directory in PowerShell. Sort-Object cmdlet will sort the File object by LastWriteTime property. [crayon-69077e5b9f0b0747415923/] [crayon-69077e5b9f0b5419412978/] The code uses the Get-ChildItem […]
20 JunePowerShell Kill Process by Name
Using Stop-Process Cmdlet Use the Stop-Process cmdlet to kill a process by name in PowerShell. [crayon-69077e5b9f2d9342138728/] The Stop-Process command stops a process by its name. The -Name parameter specifies the name of the process to stop. For example, if you want to stop the notepad program, write Stop-Process -Name "notepad". You can check all running […]