Start-Transcript – Execute only if in console and not ISE

Due to functionality differences between the PowerShell console and PowerShell ISE, there are times you only want to run certain functions and/or commands if the script is running in a console window. Below is an example of how to do so.

1
2
3
4
5
6
# Log script output to file - only works in console, not ISE.
If ($host.Name -notlike "*ISE*"){
    $scriptLog = "dcpromo_$(Get-Date -f yyyy-MM-dd_HH_mm_ss).log"
    $logging = Start-Transcript -Path $scriptLog
    Write-Host "`tTranscript started, logging to: $($logging.Path)" -ForegroundColor Cyan
}

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.