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. [crayon-69a04f58c4732198508760/] [crayon-69a04f58c4739957569269/] The Get-ChildItem is a PowerShell cmdlet that lists the items in a specified location. By default, it lists the files and directories in the current directory, but we can use […]
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. [crayon-69a04f58c4caa353935140/] 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 too many operators are not involved; otherwise, it […]
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. [crayon-69a04f58c4d6a638477255/] [crayon-69a04f58c4d6d750127921/] Microsoft’s PowerShell consists of a command-line shell and scripting language. It is based on the .NET framework for task automation and configuration management and helps power […]
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. [crayon-69a04f58c4e85106334034/] [crayon-69a04f58c4e88918904491/] The Read-Host cmdlet is the most commonly used and easy to understand. We used it to pause execution and prompt the user to get the input line […]
08 JanuaryPowerShell – Unzip File
Using Expand-Archive Cmdlet Use the Expand-Achrive cmdlet to unzip a file/folder in PowerShell. [crayon-69a04f58c50cb011996173/] 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 unzipped folder in 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. [crayon-69a04f58c5204648686239/] [crayon-69a04f58c5207762770968/] A Transcript is a saved record of all the commands and output that a PowerShell […]
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. [crayon-69a04f58c536f333008598/] [crayon-69a04f58c5372959206096/] Please note that above sum doesn’t include subfolder sizes. If you want to include subfolder as well, header over to this section. The Measure-Object cmdlet was used to calculate the property values of […]
07 JanuaryPowerShell – Read File Line by Line
Using Get-Content Cmdlet Use the Get-Content cmdlet to read file line by line in PowerShell. [crayon-69a04f58c555e343652742/] [crayon-69a04f58c5560591930066/] We used the Get-Content cmdlet to read the specified file line by line in PowerShell. It is used to get the content or data of the specified item; here, the specified item can be a text file or […]