The awk command works with text, and based on the delimiters in the output text, separates the output into columns. This separated output is displayed as columns again, using a the print function, like in C. PowerShell works a little differently.
Let us get started.
- If you did not create files, please do so. Here are some commands you could use to create the files.
PS> New-Item ./random/cities -ItemType Directory -Force
PS> Set-Location ./random/
PS> New-Item random-text.txt, himalayas.jpg, crunched-numbers.csv, bangalore.jpg, screenshot-001.png, screenshot-002.png, screenshot-003.png, demo.doc, my-plugin.rb, ./cities/mumbai.html, ./cities/nyc.html, ./cities/cairo.html, ./cities/dubai.html, ./cities/paris.html -ItemType File
- You may also want to download some real multimedia content, just so we get the length (file size) property for future use. Just download any random images or media files.
- Navigate to the location where you saved the files. I have them in a directory called random in my home directory. You would, too, if you used the script.
PS> Set-Location ./random/
Use tab-completion to complete the cmdlet as well as the path.
- List out the contents in the current location.
PS> Get-ChildItem -Path .
- Let us say, you do not require the Mode column.
PS> Get-ChildItem -Path . | Select-Object LastWriteTime, Length, Name
If you notice, select does not seem to follow the naming or capitalisation convention that PowerShell uses. How is that? Run Get-Command select to find out.
- This sequence does not really make sense to you in the current context. Shuffle the columns.
PS> Get-ChildItem -Path . | Select-Object Name, Length, LastWriteTime
That looks much better.
- Now, change the column name of LastWriteTime to Modified.
PS> Get-ChildItem -Path . | Select-Object Name, Length, @{Name='Modified'; Expression={$_.LastWriteTime}}
Notice the name of the last column now, and compare it with the previous output.
- Now, pick just the year; not the entire date.
PS> Get-ChildItem -Path . | Select-Object Name, Length, @{Name='Modified'; Expression={$_.LastWriteTime.Year}}
- See how many days have passed since the last change.
PS> Get-ChildItem -Path . | select Name, Length, @{Name='DaysSinceModification'; Expression={[math]::Round(((Get-Date) - $_.LastWriteTime).TotalDays)}}