Author: Java2blog
27 JanuaryRemove Property from Object in PowerShell
Using PSObject.Properties.Remove() Method To delete a property from an object in PowerShell, Use the New-Object cmdlet to create an object. Use the PSObject.Properties.Remove() method to remove a specific property from an object by providing the property’s name as a string parameter. $person_obj = New-Object -Typename PSCustomObject -Property @{ Name = 'Bob' Age = '25' Citizenship […]
26 JanuaryCreate File If Not Exists in PowerShell
Using the if-else Statement We can use the if-else statement to create a file as follows: Use the New-Item Cmdlet To create a file if it does not exist in PowerShell: Use the Test-Path cmdlet with the if condition to check if the file exists. Use the New-Item cmdlet to create a new file if […]
26 JanuaryCheck if Folder Exists in PowerShell
Using Built-in Cmdlets and Methods We can use built-in cmdlets and methods to check if a directory exists. Use the Test-Path Cmdlet To check if a folder exists, use the Test-Path cmdlet. $path = "C:\Windows" if(test-Path -Path $path){ Write-Host "Folder exists." }else{ Write-Host "Folder does not exist." } Folder exists. PowerShell’s Test-Path cmdlet verifies that […]
23 JanuaryConvert Month Number to Name in PowerShell
Using the Get-Culture() Cmdlet We can use the Get-Culture cmdlet in the following three possible ways: To Convert the Hardcoded Month Number to the Name To Convert the Month Number from a Date to the Name To Convert the Current Month Number to the Name Let’s learn the above-mentioned approaches one by one below. Convert […]
23 JanuaryDelete Empty Folders in PowerShell
There are multiple ways that we can use to delete the empty folders in PowerShell but before going into the details, let’s have a look at the following directory structure to know how many and which folders are empty. E:/testAlpha | |________testBeta | |___testDalta | |___testEle | |___al.txt |________testCharle |___testF | |___bl.txt |___testGiga |___testHm Now, […]
23 JanuaryPowerShell – Where-Object with Multiple Conditions
Using Where-Object Cmdlet with Multiple Conditions In PowerShell, we have various comparison and logical operators that we can use with the Where-Object cmdlet to apply multiple conditions. Let’s explore a few scenarios below. Get-ChildItem | Where-Object { ($_.extension -eq ".txt") -and ($_.length -eq 42) } Directory: E:\Test Mode LastWriteTime Length Name ---- ------------- ------ ---- […]
18 JanuaryRename Files with PowerShell
Rename Single File with PowerShell Using Rename-Item with/without the -Path Parameter Use the Rename-Item cmdlet to rename a file in PowerShell. Rename-Item "test.txt" -NewName "newtest.txt" We used the Rename-Item cmdlet to rename the test.txt file to the newtest.txt file. Note that this command will look for the file in the current directory. If you want […]
18 JanuaryPowerShell – Redirect Output to File
Using Out-File Cmdlet Use the Out-File cmdlet to redirect PowerShell output to a file. Get-Process -Name chrome | Out-File -FilePath E:\Test\OuputFile.txt The Get-Process is used to retrieve all the processes that are currently running on our machine (local computer). We can filter this information by specifying parameters; for instance, we used the -Name parameter to […]
18 JanuaryPowerShell – Logical Operators
Using Logical Operators in PowerShell PowerShell’s logical operator is a potent tool for comparing data and carrying out operations depending on those comparisons. It allows the user to design an if-then scenario in which several parameters are compared. In other words, it enables the users to make choices following the criteria they specify. A logical […]