-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdifferent-write-commands.ps1
More file actions
68 lines (48 loc) · 2.61 KB
/
different-write-commands.ps1
File metadata and controls
68 lines (48 loc) · 2.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# Revision # 1
# This version demonstrates different types of output in PowerShell.
# It shows how to use Write-Host, Write-Output, Write-Verbose, Write-Debug, Write-Warning, Write-Error, and Write-Information.
function Show-WriteTypes {
Write-Host "Write-Host : This is a message to the console (no pipeline)" -ForegroundColor Cyan
Write-Output "Write-Output : This message is sent to the pipeline"
Write-Verbose "Write-Verbose : This is a verbose message (needs -Verbose)" -Verbose
Write-Debug "Write-Debug : This is a debug message (needs -Debug)" -Debug
Write-Warning "Write-Warning : This is a warning message"
Write-Error "Write-Error : This is an error message"
Write-Information "Write-Information : This is an informational message"
}
# Run with verbose and debug switched on to see all output types
Show-WriteTypes -Verbose -Debug
# Revision # 2
# This version logs messages to a file as well as showing them in the console.
# It demonstrates how to use different write types in PowerShell and logs them to a specified file.
function Show-WriteTypes {
param (
[string]$LogFile = "C:\Temp\write-output-demo.log"
)
# Ensure log folder exists
$logDir = Split-Path $LogFile
if (-not (Test-Path $logDir)) {
New-Item -Path $logDir -ItemType Directory -Force | Out-Null
}
# Helper to write to log file
function Write-Log {
param ([string]$Message)
Add-Content -Path $LogFile -Value "$((Get-Date).ToString('s')) : $Message"
}
Write-Host "Write-Host : This is a message to the console (no pipeline)" -ForegroundColor Cyan
Write-Log "Write-Host : This is a message to the console (no pipeline)"
Write-Output "Write-Output : This message is sent to the pipeline"
Write-Log "Write-Output : This message is sent to the pipeline"
Write-Verbose "Write-Verbose : This is a verbose message (needs -Verbose)" -Verbose
Write-Log "Write-Verbose : This is a verbose message"
Write-Debug "Write-Debug : This is a debug message (needs -Debug)" -Debug
Write-Log "Write-Debug : This is a debug message"
Write-Warning "Write-Warning : This is a warning message"
Write-Log "Write-Warning : This is a warning message"
Write-Error "Write-Error : This is an error message"
Write-Log "Write-Error : This is an error message"
Write-Information "Write-Information : This is an informational message"
Write-Log "Write-Information : This is an informational message"
}
# Run with verbose and debug to show all
Show-WriteTypes -Verbose -Debug