PowerShell
25 JulyMerge Arrays in PowerShell
Using + Operator Use + operator to merge arrays in PowerShell. $array1 = @("Welcome", "to", "Java2Blog!") $array2 = @("Let's", "learn", "together.") $merged_array = $array1 + $array2 echo $merged_array Welcome to Java2Blog! Let's learn together. We used the sub-expression operator represented by @() to create two separate arrays named $array1 and $array2 (you can also create […]
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. $str = "How #$%are ^&*you?@!" $cleanedStr = [System.Text.RegularExpressions.Regex]::Replace($str, '[^\w\s]', '') Write-Host @" Original String: $str Cleaned String: $cleanedStr "@ Original String: How #$%are ^&*you?@! Cleaned String: How are you First, we declared a variable named $str and set […]
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: FirstName,LastName,Department Shruti,Mehta,Computer Science Mehvish,Ashiq,Mathematics John,Christopher,Psychology Saira,Daniel,Humanities Gurmeet,Kaur,Arts 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 […]
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. $string = "Hi! Welcome to Java2Blog! Let's learn." $newString = ($string.Split("!"))[0] Write-Output $newString Hi We declared and initialized the $string variable with "Hi! Welcome to Java2Blog! Let's learn." value. Next, we chained the Split() method […]
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. Get-ChildItem -Path "E:\Test" -Filter "file*" Directory: E:\Test Mode LastWriteTime Length Name ---- ------------- ------ ---- d----- 4/25/2023 7:22 AM File -a---- 3/10/2023 12:42 AM 33629 file - Copy.doc -a---- 3/11/2023 […]
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 $Filename = "C:\Test1\File1.txt" $newFilename = "C:\Test1\File1_$(Get-Date -Format yyyy-MM-dd).txt" Rename-Item -Path $Filename -NewName $newFilename Write-Host "New filename: $newFilename" New […]
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. Get-ChildItem -Path C:\Test1 | Sort-Object LastWriteTime -Descending […]
20 JunePowerShell Kill Process by Name
Using Stop-Process Cmdlet Use the Stop-Process cmdlet to kill a process by name in PowerShell. Stop-Process -Name "process_name" 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 […]