Author: Java2blog
13 JanuaryShow Hidden Files (dotfiles) with Windows PowerShell
Using Get-ChildItem with the -Force Parameter To show hidden files with other files, use the Get-ChildItem cmdlet with the -Force or -fo parameter. Get-ChildItem -Force Directory: D:\temp Mode LastWriteTime Length Name ---- ------------- ------ ---- -a---- 12/31/2022 3:21 AM 0 a.txt -a-h-- 12/31/2022 3:21 AM 0 b.txt -a---- 12/31/2022 3:21 AM 0 c.txt The Get-ChildItem […]
13 JanuaryMerge Multiple CSV Files in PowerShell
Using Import-Csv and Export-Csv Before merging multiple .csv files, let’s write a script to see how many .csv files are in the specified directory. For that, create a variable called $sourcefiles containing a list of all the CSV files in the directory. Next, use the Get-ChildItem cmdlet to retrieve all the CSV files in the […]
10 JanuaryPowerShell – Multiline Command
Using Backticks Use backticks to split multiline commands in PowerShell. Get-ChildItem -Recurse ` -Filter *.md ` | Select LastWriteTime Usually, we get an automatic line continuation in PowerShell when the command can’t syntactically be complete. However, if splitting a multiline command is required, then using backticks is preferred in such scenarios as presented above where […]
08 JanuaryPowerShell – Call Function with Parameters
Using Positional Parameters To call a function, use positional parameters separated by space. Please note that you need to pass parameters in sequence while calling the function. function getEmployee{ param( $name, $age, $salary ) "name: $name" "age: $age" "salary: $salary" } getEmployee "Anonymous" 20 80000 name: Anonymous age: 20 salary: 80000 Microsoft’s PowerShell consists of […]
08 JanuaryPress Any Key to Continue in PowerShell
Solutions for PowerShell Integrated Scripting Environment(ISE) and Command Line Console Using Read-Host Cmdlet Use the Read-Host command to enable press any key to continue in PowerShell. Read-Host -Prompt "Press any key to continue..." Press any key to continue...: pressed pressed The Read-Host cmdlet is the most commonly used and easy to understand. We used it […]
08 JanuaryPowerShell – Unzip File
Using Expand-Archive Cmdlet Use the Expand-Achrive cmdlet to unzip a file/folder in PowerShell. Expand-Archive -Path E:\Test\FolderA.zip -DestinationPath E:\ExtractedFolderA Please note that Expand-Archive cmdlet is available from PowerShell 5 onwards. Print envionment variable $PSVersionTable.PSVersion to check version of your PowerShell. We used the Expand-Achrive cmdlet to unzip a folder from the given location and save the […]
07 JanuaryPowerShell – Write to Log File
Using the Transcript To write to a log file using Transcript: Use the Start-Transcript cmdlet to start writing to the log file. Perform operations normally without writing anything to the file. Use the Stop-Transcript to stop the transcript. Start-Transcript "D:\Logs\output.log" -Append 2+2 Write-Output "Logging..." Stop-Transcript Get-Content "D:\Logs\output.log" Transcript started, output file is D:\Logs\output.log 4 Logging... […]
07 JanuaryPowerShell – Get Size of Folder
Using Get-ChildItem with Measure-Object Use Get-ChildItem with the Measure-Object cmdlet to get the size of files in the specified directory. Get-ChildItem C:\Users\DELL\Desktop\work | Measure-Object -Property Length -sum Count: 16 Average: Sum: 208664 Maximum: Minimum: Property: Length Please note that above sum doesn’t include subfolder sizes. If you want to include subfolder as well, header over […]
07 JanuaryPowerShell – Read File Line by Line
Using Get-Content Cmdlet Use the Get-Content cmdlet to read file line by line in PowerShell. Get-Content E:\Test\File1.txt This is line one. This is line two This is line 3. I am line 4. Its line 5. This is line 6. This is line 7. We used the Get-Content cmdlet to read the specified file line […]