# how to log the powershellactivity

**URL:** <https://forums.powershell.org/t/how-to-log-the-powershellactivity/12353>\
**Category:** PowerShell Help\
**Created:** [April 6, 2019, 6:48am UTC](https://forums.powershell.org/t/how-to-log-the-powershellactivity/12353 "2019-04-06T06:48:36Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![gval](https://avatars.discourse-cdn.com/v4/letter/g/5f8ce5/32.png) [@gval](https://forums.powershell.org/u/gval)\
**Post date:** [April 6, 2019, 6:48am UTC](https://forums.powershell.org/t/how-to-log-the-powershellactivity/12353/1 "2019-04-06T06:48:36Z")

</div>

I am running a PS script to stop and restart Services using **Restart-Service**. For troubleshooting issues I’d like to write the output of every activity to a log file so we can make sure the services stopped and restart.

please find the code:

```
$mailprops= @{

From = 'operating.system@abc.com'

To = 'asdf@abc.com'

Subject = 'test'

Attachment = $logfileSmtpServer = '127.0.0.1'
}

'Stopping services' | Out-File $logfile

Stop-Service '3456','2354' 'Services stopped' | Out-File $logfile -append

Restart-computer QY34-Force 'Remote computer sent restart command' | Out-File $logfile -append

Send-MailMessage @mailprops
```

can any one suggest me how to fix this?

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [April 6, 2019, 6:59am UTC](https://forums.powershell.org/t/how-to-log-the-powershellactivity/12353/2 "2019-04-06T06:59:31Z")

</div>

Could you please format your code as code using the code tag button “pre” on the edit bar of the post editor? Thanks

You could use the output of Get-Service to show the change in the status …

```
$mailprops = @{
From = ‘operating.system@abc.com’
To = ‘asdf@abc.com’
Subject = ‘test’
Attachment = $logfile
SmtpServer = ‘127.0.0.1’
}

‘Stopping services - status before’ | Out-File -FilePath $logfile  
Get-Service -Name ‘3456’, ‘2354’ | Out-File -FilePath $logfile -Append  
Stop-Service ‘3456’, ‘2354’  
‘Stopping services - status after’ | Out-File -FilePath $logfile -Append  
Get-Service -Name ‘3456’, ‘2354’ | Out-File -FilePath $logfile -Append  
Restart-Computer -ComputerName QY34 –Force  
‘Remote computer sent restart command’ | Out-File -FilePath $logfile -Append

Send–MailMessage @mailprops
```

---

<div class="post-metadata">

**Author:** ![sam-boutros](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/sam-boutros/32/5_2.png) [@sam-boutros](https://forums.powershell.org/u/sam-boutros)\
**Post date:** [April 6, 2019, 7:01am UTC](https://forums.powershell.org/t/how-to-log-the-powershellactivity/12353/3 "2019-04-06T07:01:12Z")

</div>

[https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.host/start-transcript?view=powershell-6](https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.host/start-transcript?view=powershell-6)

---

<div class="post-metadata">

**Author:** ![pwshliquori](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/pwshliquori/32/222_2.png) [@pwshliquori](https://forums.powershell.org/u/pwshliquori)\
**Post date:** [April 6, 2019, 7:15am UTC](https://forums.powershell.org/t/how-to-log-the-powershellactivity/12353/4 "2019-04-06T07:15:30Z")

</div>

I agree with Sam. Start-Transcript is a good way to log the PowerShell session when a script is running.

```
Start-Transcript -Path C:\temp\script.log
# Code goes here
Stop-Transcript
```

pwshliquori

---

<div class="post-metadata">

**Author:** ![bkindle](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/bkindle/32/95_2.png) [@bkindle](https://forums.powershell.org/u/bkindle)\
**Post date:** [April 6, 2019, 7:30am UTC](https://forums.powershell.org/t/how-to-log-the-powershellactivity/12353/5 "2019-04-06T07:30:19Z")

</div>

[quote quote=149936][https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.host/start-transcript?view=powershell-6](https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.host/start-transcript?view=powershell-6)

[/quote]

Transcripts are useful but you end up getting much more info than what might be necessary.

&nbsp;

As for the script, you may want to also look at building in some form of error handling as well. This script as it stands right now may produce errors that are not logged. I would suggest reading up on how to use ‘Try / Catch / Finally’ and make this a function that you can feed parameters to, such as service or computer name, and even the path to the log file you want to keep.

---

<div class="post-metadata">

**Author:** ![sam-boutros](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/sam-boutros/32/5_2.png) [@sam-boutros](https://forums.powershell.org/u/sam-boutros)\
**Post date:** [April 6, 2019, 1:21pm UTC](https://forums.powershell.org/t/how-to-log-the-powershellactivity/12353/6 "2019-04-06T13:21:46Z")

</div>

Well, if you don’t want to use start-transcript, you can use Write-Log  
This is a function that I wrote years back and is now part of the AZSBTools PS module ([install-module AZSBTools](https://www.powershellgallery.com/packages/AZSBTools/))  
It writes output to the console/and optionally to a log file - it also writes to the console in color lol  
Example:

```
Install-Module AZSBTools -Force -AllowClobber 

$logfile = "$env:TEMP\Restarting services report - $(Get-Date -Format 'ddMMMMyyyy_hh-mm-ss_tt').txt"
$ComputerName = 'QY34'

$mailprops = @{
    From = 'operating.system@abc.com'
    To = 'asdf@abc.com'
    Subject = 'test'
    Attachment = $logfile
    SmtpServer = '127.0.0.1' 
}

Write-Log 'Stopping services - status before:' Green $logfile
Write-Log (Get-Service -Name '3456', '2354' | Out-String).Trim() Cyan $logfile

Stop-Service '3456', '2354'
Write-Log 'Stopping services - status after:' Green $logfile
Write-Log (Get-Service -Name '3456', '2354' | Out-String).Trim() Cyan $logfile

Restart-Computer -ComputerName $ComputerName –Force 
Write-Log 'Remote computer',$ComputerName,'sent restart command' Green,Cyan,Green $logfile

Send–MailMessage @mailprops
```

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [April 6, 2019, 9:16pm UTC](https://forums.powershell.org/t/how-to-log-the-powershellactivity/12353/7 "2019-04-06T21:16:19Z")

</div>

It seems like gval19 does not like the answers he’s got here … 😉😃

[how to log the powershellactivity](https://social.technet.microsoft.com/Forums/en-US/171e070b-b1bb-44d1-a755-21aa5578b55d/how-to-log-the-powershellactivity?forum=ITCG).

---

<div class="post-metadata">

**Author:** ![js2010](https://avatars.discourse-cdn.com/v4/letter/j/ebca7d/32.png) [@js2010](https://forums.powershell.org/u/js2010)\
**Post date:** [April 7, 2019, 9:04am UTC](https://forums.powershell.org/t/how-to-log-the-powershellactivity/12353/8 "2019-04-07T09:04:51Z")

</div>

The -passthru option returns an ServiceController object for those commands. You don’t need to run stop-service first.

```
PS C:\Users\admin> stop-service apple* -passthru

Status Name DisplayName
------ ---- -----------
Stopped Apple Mobile De... Apple Mobile Device Service

PS C:\Users\admin> restart-service apple* -passthru

Status Name DisplayName
------ ---- -----------
Running Apple Mobile De... Apple Mobile Device Service
```

---

<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:33pm UTC](https://forums.powershell.org/t/how-to-log-the-powershellactivity/12353/9 "2024-05-16T20:33:02Z")

</div>


