# Logging function

**URL:** https://forums.powershell.org/t/logging-function/513
**Category:** PowerShell Help
**Created:** [December 31, 2011, 6:30pm UTC](https://forums.powershell.org/t/logging-function/513 "2011-12-31T18:30:00Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![archives](https://avatars.discourse-cdn.com/v4/letter/a/bb73d2/32.png) [@archives](https://forums.powershell.org/u/archives)
#### Post date: [December 31, 2011, 6:30pm UTC](https://forums.powershell.org/t/logging-function/513/1 "2011-12-31T18:30:00Z")

</div>

by aussiebenfica at 2013-01-21 23:45:33

> Hello all,  
>   
> This is my first post, I am new to Powershell / scripting. I did a powershell course at the end of last year and am now trying to get some of it into practice.  
>   
> I am creating a copy script to grab files from several locations on my network, i have some limited logging.  
>   
> I would like to actually log all the commands with a time stamp and what was done… I have found some ideas on the internet, a coleague gave me the below funtion, but i just cant understand how to implement it into a copy script, see example below…  
>   
> Function…  
>   
> function write-log ([string] $severity, [string] $message)  
> {  
> $ts = get-date -format o  
> $sev = $severity.ToUpper().Substring(0,1)  
> $log = "C:\Temp\log.log"  
> $line = "$ts $sev $message"  
> if ($sev -eq "E")  
> {  
> write-error $line   
> }  
> else   
> {  
> write-output $line  
> }  
> $line \>\> $log  
> }  
>   
>   
> Example of my copy script…  
>   
> # import script  
>   
> # Data / Source / Destination / Values  
> $ImportFolder = "c:\Imports&quot;  
> $log = "C:\Imports\Logs\Import-$(get-date -format yyyyddMM).log"  
> #$time = get-date -format HH:mm:ss  
>   
>   
> $SOURCE1 = "\networklocation\export$&quot;  
> $SOURCE1\_file\_list = "file1.csv",  
> "file2.csv",  
> "file3.csv"  
>   
> foreach ($file in $SOURCE1\_file\_list)  
> {  
> $str = "File: $file Result: "  
> try{  
> copy-item $SOURCE1$file $ImportFolder -ea stop  
> $str += "Success"  
> }  
> catch  
> {  
> $str += "Failed - $($_.exception.message)"  
> }  
> $str | out-file $log -append  
> }_  
>   
> _$SOURCE2 = "\networklocation2\export$&quot;  
> $SOURCE2\_file\_list = "file4.csv",  
> "file5.csv",  
> "file6.csv"_  
>   
> _foreach ($file in $SOURCE2\_file\_list)  
> {  
> $str = "File: $file Result: "  
> try{  
> copy-item $SOURCE2$file $ImportFolder -ea stop  
> $str += "Success"  
> }  
> catch  
> {  
> $str += "Failed - $($_.exception.message)"  
> }  
> $str | out-file $log -append  
> }  
>   
> Apologies in advance if this is too long a post , or in the wrong section.  
>   
> Any help would be greatly appreciated, Many Thanks…  
>   
> AB

by DonJ at 2013-01-22 05:56:21

> Welcome.  
>   
> As a note, you can sue the CODE or POWERSHELL buttons in the toolbar to surround your code and provide better formatting.  
>   
> The logging function would go at the top of your script. You simply call it whenever you want to write something to the log. For example, I might do so immediately inside your Try{} construct to log the file I was about to copy, and then inside the Catch{} construct to log any errors.  
>   
> `Write-Log "Severe" "$file failed"`  
>   
> It looks like your script is already creating a log, though, including errors. I’m not sure what you’re looking for in addition to that. There’s no PowerShell capability for simply logging every command that’s run; you have to write the code to create your log and what goes into it.

by ArtB0514 at 2013-01-22 07:35:35

> Actually, you can log the activity of a script using the Start-Transcript command. It records both the commands that are executed and any output sent to the host from the point where it is executed.

by DonJ at 2013-01-22 08:00:22

> That’s only true if you’re running in the Console host. Keep in mind that other hosts, including the ISE, may not support Start-Transcript. I just want to point that out, since someone working on a script may well be working in the ISE.

by aussiebenfica at 2013-02-04 01:09:10

> Hi Guys, Thanks for the replies…  
>   
> I ended up doing the below , this allowed me to get exactly what i need.  
>   
>   
> foreach ($file in $SOURCE1\_file\_list)  
> {  
> $str = Get-Date -format HH:mm:ss  
> $str += " -\> $file Result: "  
> try{  
> copy-item $SOURCE1$file $ImportFolder -ea stop  
> $str += " Success"  
> }  
> catch  
> {  
> $str += "Failed - $($\_.exception.message)"  
> }  
> $str | Out-File $log -append -Encoding ASCII  
>   
> This allowed me to have a time stamp on each line of my log.  
>   
> Cheers  
>   
> AB

---

<div class="post-metadata">

### Author: ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)
#### Post date: [May 16, 2024, 8:58pm UTC](https://forums.powershell.org/t/logging-function/513/2 "2024-05-16T20:58:25Z")

</div>


