1
1
---
2
- ms.date : 2017-06-09
2
+ ms.date : 2017-12-05
3
3
schema : 2.0.0
4
4
locale : en-us
5
5
keywords : powershell,cmdlet
@@ -248,7 +248,7 @@ debug message is not displayed and processing continues. The final command
248
248
uses the Debug parameter to override the preference for a single command.
249
249
250
250
``` powershell
251
- PS> $debugpreference # Get the current value of \ $DebugPreference
251
+ PS> $debugpreference # Get the current value of $DebugPreference
252
252
SilentlyContinue
253
253
254
254
PS> write-debug "Hello, World"
@@ -301,7 +301,7 @@ uses the Debug parameter with a value of \$false to suppress the message for
301
301
a single command.
302
302
303
303
``` powershell
304
- PS> \ $debugpreference = "Inquire"
304
+ PS> $debugpreference = "Inquire"
305
305
PS> write-debug "Hello, World"
306
306
DEBUG: Hello, World
307
307
@@ -375,7 +375,7 @@ This example shows the effect of the SilentlyContinue value.
375
375
376
376
``` powershell
377
377
PS> # Change the value of the preference.
378
- PS> \ $ErrorActionPreference = "SilentlyContinue"
378
+ PS> $ErrorActionPreference = "SilentlyContinue"
379
379
380
380
PS> # Generate an error message.
381
381
PS> write-error "Hello, World"
@@ -393,14 +393,14 @@ a non-existent file, nofile.txt. The example also uses the ErrorAction common
393
393
parameter to override the preference.
394
394
395
395
``` powershell
396
- PS> \ $erroractionpreference
396
+ PS> $erroractionpreference
397
397
SilentlyContinue # Display the value of the preference.
398
398
399
399
PS> get-childitem -path nofile.txt
400
400
PS> # Error message is suppressed.
401
401
402
402
PS> # Change the value to Continue.
403
- PS> \ $ErrorActionPreference = "Continue"
403
+ PS> $ErrorActionPreference = "Continue"
404
404
405
405
PS> get-childitem -path nofile.txt
406
406
Get-ChildItem : Cannot find path 'C:\nofile.txt' because it does not exist.
@@ -412,7 +412,7 @@ PS> get-childitem -path nofile.txt -erroraction SilentlyContinue
412
412
PS> # Error message is suppressed.
413
413
414
414
PS> # Change the value to Inquire.
415
- PS> \ $ErrorActionPreference = "Inquire"
415
+ PS> $ErrorActionPreference = "Inquire"
416
416
PS> get-childitem -path nofile.txt
417
417
418
418
Confirm
@@ -425,7 +425,7 @@ At line:1 char:4
425
425
+ get-childitem <<<< nofile.txt
426
426
427
427
PS> # Change the value to Continue.
428
- PS> \ $ErrorActionPreference = "Continue"
428
+ PS> $ErrorActionPreference = "Continue"
429
429
PS> Get-Childitem nofile.txt -erroraction "Inquire"
430
430
PS> # Use the ErrorAction parameter to override the preference value.
431
431
@@ -462,7 +462,7 @@ NormalView. In this case, the Get-ChildItem command is used to find a
462
462
non-existent file.
463
463
464
464
``` powershell
465
- PS> \ $ErrorView # Verify the value.
465
+ PS> $ErrorView # Verify the value.
466
466
NormalView
467
467
468
468
PS> get-childitem nofile.txt # Find a non-existent file.
@@ -475,7 +475,7 @@ This example shows how the same error appears when the value of
475
475
\$ ErrorView is CategoryView.
476
476
477
477
``` powershell
478
- PS> \ $ErrorView = "CategoryView" # Change the value to
478
+ PS> $ErrorView = "CategoryView" # Change the value to
479
479
CategoryView
480
480
481
481
PS> get-childitem nofile.txt
@@ -493,7 +493,7 @@ error in the error array (element 0) and formats all of the properties of the
493
493
error object in a list.
494
494
495
495
``` powershell
496
- PS> \ $error[0] | format-list -property * -force
496
+ PS> $error[0] | format-list -property * -force
497
497
498
498
Exception : System.Management.Automation.ItemNotFoundException: Cannot
499
499
find path 'C:\nofile.txt' because it does not exist.
@@ -546,10 +546,10 @@ In the resulting display, the list in the Group column is now limited by the
546
546
line length. In the final command in the example, use the Wrap parameter of
547
547
Format-Table to display all of the processes in each Status group.
548
548
549
- PS> \$ formatenumerationlimit # Find the current value
549
+ ``` powershell
550
+ PS> $formatenumerationlimit # Find the current value
550
551
4
551
552
552
- ``` powershell
553
553
PS> # List all services grouped by status
554
554
PS> get-service | group-object -property status
555
555
@@ -560,7 +560,7 @@ Count Name Group
560
560
PS> # The list is truncated after 4 items.
561
561
562
562
PS> # Increase the limit to 1000.
563
- PS> \ $formatenumerationlimit = 1000
563
+ PS> $formatenumerationlimit = 1000
564
564
PS> get-service | group-object -property status
565
565
566
566
Count Name Group
@@ -650,20 +650,20 @@ The Log*Event preference variables are as follows:
650
650
To enable a Log* Event, type the variable with a value of \$ true, for example:
651
651
652
652
``` powershell
653
- \ $LogCommandLifeCycleEvent
653
+ $LogCommandLifeCycleEvent
654
654
```
655
655
656
656
- or -
657
657
658
658
``` powershell
659
- \ $LogCommandLifeCycleEvent = \ $true
659
+ $LogCommandLifeCycleEvent = $true
660
660
```
661
661
662
662
To disable an event type, type the variable with a value of \$ false, for
663
663
example:
664
664
665
665
``` powershell
666
- \ $LogCommandLifeCycleEvent = \ $false
666
+ $LogCommandLifeCycleEvent = $false
667
667
```
668
668
669
669
The events that you enable are effective only for the current PowerShell
@@ -719,26 +719,26 @@ To count the errors on your system, use the Count property of the \$Error
719
719
array. Type:
720
720
721
721
``` powershell
722
- \ $Error.count
722
+ $Error.count
723
723
```
724
724
725
725
To display a specific error, use array notation to display the error. For
726
726
example, to see the most recent error, type:
727
727
728
728
``` powershell
729
- \ $Error[0]
729
+ $Error[0]
730
730
```
731
731
732
732
To display the oldest retained error, type:
733
733
734
734
``` powershell
735
- \ $Error[(\ $Error.Count -1]
735
+ $Error[($Error.Count -1]
736
736
```
737
737
738
738
To display the properties of the ErrorRecord object, type:
739
739
740
740
``` powershell
741
- \ $Error[0] | format-list -property * -force
741
+ $Error[0] | format-list -property * -force
742
742
```
743
743
744
744
In this command, the Force parameter overrides the special formatting of
@@ -748,11 +748,11 @@ To delete all errors from the error history, use the Clear method of the error
748
748
array.
749
749
750
750
``` powershell
751
- PS> \ $Error.count
751
+ PS> $Error.count
752
752
17
753
- PS> \ $Error.clear()
753
+ PS> $Error.clear()
754
754
PS>
755
- PS> \ $Error.count
755
+ PS> $Error.count
756
756
0
757
757
```
758
758
@@ -844,19 +844,19 @@ is converted to a string. In this case, an array of integers is stored in a
844
844
variable and then the variable is cast as a string.
845
845
846
846
``` powershell
847
- PS> \ $array = 1,2,3 # Store an array of integers.
847
+ PS> $array = 1,2,3 # Store an array of integers.
848
848
849
- PS> [string]\ $array # Cast the array to a string.
849
+ PS> [string]$array # Cast the array to a string.
850
850
1 2 3 # Spaces separate the elements
851
851
```
852
852
853
853
To change the separator, add the \$ OFS variable by assigning a value to it.
854
854
To work correctly, the variable must be named \$ OFS.
855
855
856
856
``` powershell
857
- PS> \ $OFS = "+" # Create \ $OFS and assign a "+"
857
+ PS> $OFS = "+" # Create $OFS and assign a "+"
858
858
859
- PS> [string]\ $array # Repeat the command
859
+ PS> [string]$array # Repeat the command
860
860
1+2+3 # Plus signs separate the elements
861
861
```
862
862
@@ -865,10 +865,10 @@ To restore the default behavior, you can assign a space (" ") to the value of
865
865
verifies that the separator is a space.
866
866
867
867
``` powershell
868
- PS> Remove-Variable OFS # Delete \ $OFS
868
+ PS> Remove-Variable OFS # Delete $OFS
869
869
PS>
870
870
871
- PS> [string]\ $array # Repeat the command
871
+ PS> [string]$array # Repeat the command
872
872
1 2 3 # Spaces separate the elements
873
873
```
874
874
@@ -897,7 +897,7 @@ The first command finds the value of \$OutputEncoding. Because the value is an
897
897
encoding object, display only its EncodingName property.
898
898
899
899
``` powershell
900
- PS> \ $OutputEncoding.EncodingName # Find the current value US-ASCII
900
+ PS> $OutputEncoding.EncodingName # Find the current value US-ASCII
901
901
```
902
902
903
903
In this example, a FINDSTR command is used to search for two Chinese
@@ -918,9 +918,9 @@ locale selected for Windows. Because OutputEncoding is a static property of
918
918
the console, use double-colons (::) in the command.
919
919
920
920
``` powershell
921
- PS> \ $OutputEncoding = [console]::outputencoding
921
+ PS> $OutputEncoding = [console]::outputencoding
922
922
PS> # Set the value equal to the OutputEncoding property of the console.
923
- PS> \ $OutputEncoding.EncodingName
923
+ PS> $OutputEncoding.EncodingName
924
924
OEM United States
925
925
```
926
926
As a result of this change, the FINDSTR command finds the characters.
@@ -1100,7 +1100,7 @@ values you prefer. Save the output in a variable called \$PSSessionOption.
1100
1100
For example,
1101
1101
1102
1102
``` powershell
1103
- \ $PSSessionOption = New-PSSessionOption -NoCompression
1103
+ $PSSessionOption = New-PSSessionOption -NoCompression
1104
1104
```
1105
1105
1106
1106
To use the \$ PSSessionOption preference variable in every PowerShell session,
@@ -1202,7 +1202,7 @@ This example shows the effect of the Inquire value.
1202
1202
1203
1203
``` powershell
1204
1204
PS> # Change the value to Inquire.
1205
- PS> \ $VerbosePreference = "Inquire"
1205
+ PS> $VerbosePreference = "Inquire"
1206
1206
PS> Write-Verbose "Verbose message test."
1207
1207
VERBOSE: Verbose message test.
1208
1208
Confirm
@@ -1249,7 +1249,7 @@ value.
1249
1249
This example shows the effect of the Continue value, which is the default.
1250
1250
1251
1251
``` powershell
1252
- PS> \ $WarningPreference # Find the current value.
1252
+ PS> $WarningPreference # Find the current value.
1253
1253
Continue
1254
1254
PS> Write-Warning "This action can delete data."
1255
1255
WARNING: This action can delete data.
@@ -1263,7 +1263,7 @@ This example shows the effect of the SilentlyContinue value.
1263
1263
1264
1264
``` powershell
1265
1265
PS> # Change the value to SilentlyContinue.
1266
- PS> \ $WarningPreference = "SilentlyContinue"
1266
+ PS> $WarningPreference = "SilentlyContinue"
1267
1267
PS> Write-Warning "This action can delete data."
1268
1268
PS> # Write a warning message.
1269
1269
@@ -1280,7 +1280,7 @@ This example shows the effect of the Inquire value.
1280
1280
1281
1281
``` powershell
1282
1282
PS> # Change the value to Inquire.
1283
- PS> \ $WarningPreference = "Inquire"
1283
+ PS> $WarningPreference = "Inquire"
1284
1284
PS> Write-Warning "This action can delete data."
1285
1285
WARNING: This action can delete data.
1286
1286
@@ -1299,7 +1299,7 @@ This example shows the effect of the Stop value.
1299
1299
1300
1300
``` powershell
1301
1301
PS> # Change the value to Stop.
1302
- PS> \ $WarningPreference = "Stop"
1302
+ PS> $WarningPreference = "Stop"
1303
1303
1304
1304
PS> Write-Warning "This action can delete data."
1305
1305
WARNING: This action can delete data.
@@ -1360,7 +1360,7 @@ This example shows the effect of the 0 (not enabled) value, which is the
1360
1360
default.
1361
1361
1362
1362
``` powershell
1363
- PS> \ $whatifpreference
1363
+ PS> $whatifpreference
1364
1364
0 # Check the current value.
1365
1365
1366
1366
PS> # Verify that the file exists.
@@ -1400,8 +1400,8 @@ Remove-Item to delete a cmdlet, Remove-Item displays the path to the file that
1400
1400
it would delete, but it does not delete the file.
1401
1401
1402
1402
``` powershell
1403
- PS> \ $whatifpreference = 1
1404
- PS> \ $whatifpreference
1403
+ PS> $whatifpreference = 1
1404
+ PS> $whatifpreference
1405
1405
1 # Change the value.
1406
1406
1407
1407
PS> # Try to delete a file.
@@ -1417,8 +1417,8 @@ This example shows how to delete a file when the value of \$WhatIfPreference
1417
1417
is 1. It uses the WhatIf parameter with a value of \$ false.
1418
1418
1419
1419
``` powershell
1420
- PS> # Use the WhatIf parameter with \ $false.
1421
- PS> remove-item test.txt -whatif:\ $false
1420
+ PS> # Use the WhatIf parameter with $false.
1421
+ PS> remove-item test.txt -whatif:$false
1422
1422
```
1423
1423
1424
1424
This example demonstrates that some cmdlets support WhatIf behavior and others
@@ -1430,7 +1430,7 @@ a value of \$false.
1430
1430
1431
1431
``` powershell
1432
1432
PS> # Change the value to 1.
1433
- PS> \ $whatifpreference = 1
1433
+ PS> $whatifpreference = 1
1434
1434
1435
1435
PS> get-process winword
1436
1436
A Get-Process command completes.
@@ -1443,8 +1443,8 @@ PS> # A Stop-Process command uses WhatIf.
1443
1443
PS> stop-process -name winword
1444
1444
What if: Performing operation "Stop-Process" on Target "WINWORD (2312)".
1445
1445
1446
- PS> stop-process -name winword -whatif:\ $false
1447
- PS> # WhatIf:\ $false overrides the preference.
1446
+ PS> stop-process -name winword -whatif:$false
1447
+ PS> # WhatIf:$false overrides the preference.
1448
1448
1449
1449
PS> # Verify that the process is stopped.
1450
1450
PS> get-process winword
0 commit comments