Using PowerShell To Create Log/CSV Files And File Out Commands

In many instances, while developing using Powershell, we need to get the logfile in a specific folder and format. This article talks about the basic file out cmdlets and other options to export the Powershell value from console to file format. The first and foremost important way to learn powershell is to use Get-Help cmdlet. Before we start working with powershell commands, it is always a best pratice to update the help cmdlets which will update all the necessary packages of latest releases.
Use this command - Update-Help ,


Update will fail if it is not running as adminstrator.

All PowerShell cmdlets are in a particular pattern; in general, they are verb-noun, example Get-Help, Get-Members, New-Item, etc.if one is able to use the help cmdlet effectively any operation can be performed. Now we need the date in order to create the log, to get the date [assuming we don't know the cmdlet]  we can always use the help [alias of Get-Help ] and add wildcard search to get the particular cmdlet. 
By running the cmdlet - help *date*, PowerShell fetches all the cmdlets which contain the word date, so now the particular cmdlet which displays the current date is "Get-Date"
Another important cmdlet to get all the methods and properties of any cmdlet is to use get-members, as shown in the below screenshot.

Just as in  .net framework, we use the methods and property function. For assing the output of any powershell command we use $var, for example $date = Get-Date. This function output is stored in the variable $date which has all the methods and properties as seen in the screenshot.
Now in order to create the logfile, we need to create new text file and add content to the file, which can be done using the following commands,
  1. $date = Get-Date -Format yyyy-MM-dd  
  2. $logfilename = "Log "+$date  
  3. $filepath = "C:\"+$logfilename+".txt"  
  4. New-Item -Path $filepath  -ItemType file  
  5. Add-Content -Path $filepath -Value "Hello World"  
  6. Invoke-Item -Path $filepath  
Output of the script - creates a file in today's date and adds content and invokes the text file output.
In PowerShell output of one command can be piped to other cmdlets, it can be done using the operator " | ", which is a useful command to get specific properties of objects or for filtering it.


Get-Process | select ProcessName,CPU or Get-Process | foreach-Object *** etc
There are many file out commands to get the output in any particular formats, Examples - Export-csv, File-Out,Out-Gridview.
This was a basic walkthrough of powershell cmdlets and the use of help, filtering and creating log files.