Using PowerShell create a log file

Create log file in PowerShell in 2 proven steps

No comments

Loading

Create log file in PowerShell – In this post, I will show how to build or create a logging function in PowerShell in the PowerShell “.ps1” file directory. However, we can change to any directory wherever we wish – only we need to change the directory path.

Create log file in PowerShell – how to create a log file for the PowerShell scripting or coding?

Using the below PowerShell script we can create a log file in PowerShell:

cls
$fileName = "File_Uploading_Report"
$enddate = (Get-Date).tostring("yyyyMMddhhmmss") 
$logFileName = $fileName +"_"+ $enddate+"_Log.txt"   
$invocation = (Get-Variable MyInvocation).Value  
$directoryPath = Split-Path $invocation.MyCommand.Path

$directoryPathForLog=$directoryPath+"\"+"LogFiles"
if(!(Test-Path -path $directoryPathForLog))  
        {  
            New-Item -ItemType directory -Path $directoryPathForLog
               
        }

$logPath = $directoryPathForLog + "\" + $logFileName
$isLogFileCreated = $False 

function Write-Log([string]$logMsg)  
{   
    if(!$isLogFileCreated){   
        Write-Host "Creating Log File..."   
        if(!(Test-Path -path $directoryPath))  
        {  
            Write-Host "Please Provide Proper Log Path" -ForegroundColor Red   
        }   
        else   
        {   
            $script:isLogFileCreated = $True   
            Write-Host "Log File ($logFileName) Created..."   
            [string]$logMessage = [System.String]::Format("[$(Get-Date)] - {0}", $logMsg)   
            Add-Content -Path $logPath -Value $logMessage   
        }   
    }   
    else   
    {   
        [string]$logMessage = [System.String]::Format("[$(Get-Date)] - {0}", $logMsg)   
        Add-Content -Path $logPath -Value $logMessage   
    }   
}

#####How to use?################################ 
$sampleLogMessage="This is a sample log entry";
Write-Log $sampleLogMessage
Write-host "Log file entry has been successfully tested"
#####How to use ends here#######################

Create log file in PowerShell: Create a log file for the PowerShell scripting or coding – Example

Create log file in PowerShell

Summary: Create log file in PowerShell

Thus, in this post, we have seen how to create a log file using a PowerShell function. And also learned the use of the below PowerShell command and their usages:

  • (get-variable myinvocation).value for knowing the source path of an executing script.
  • How to create Function in PowerShell and its usage.
  • How to create a log file in PowerShell Script.
  • New-Item – Create Folders & Files using PowerShell Basics.
  • Script write-Log PowerShell Logging Function.
  • Test-Path – Folder Creation PowerShell.

See Also: PowerShell tutorial for beginners

You may also like the below PowerShell tutorials

Download SharePoint Online PDF Book

Download SharePoint Online & Office 365 Administration eBook

Buy the premium version of SharePoint Online & Office 365 administration eBook from here:



Buy SharePoint Online & Office 365 Administration eBook


 

Get the free demo PDF eBook from here:

FREE DOWNLOAD

Send download link to:

Subscribe to get exclusive content and recommendations every month. You can unsubscribe anytime.

About Post Author

Do you have a better solution or question on this topic? Please leave a comment