You can use PowerShell redirection operators to redirect a script output to a file. Below is a sort example for your reference –
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
## Create a powershell script @' echo "doing something" Write-Warning -Message "some warnings" echo "doing something erroneous" Write-Error -Message "some errors" '@ > myscript.ps1 ## Execute the script .\myscript.ps1 *>&1 > mylog.log ## Redirection options ## Pipeline (1) ## Error (2) ## Warning (3) ## Verbose (4) ## Debug (5) ## All (*) ## > Redirect to a file and replace contents ## >> Redirect to a file and append to existing content ## >&1 Merge with pipeline output ## Get script execution logs Get-Content .\mylog.log |