Skip to content

Commit c542bce

Browse files
Adding example to -AddToHistoryHandler (#10269)
* Adding example to -AddToHistoryHandler * adding note about profile
1 parent d8239d7 commit c542bce

File tree

4 files changed

+116
-4
lines changed

4 files changed

+116
-4
lines changed

reference/5.1/PSReadLine/Set-PSReadLineOption.md

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
external help file: Microsoft.PowerShell.PSReadLine2.dll-Help.xml
33
Locale: en-US
44
Module Name: PSReadline
5-
ms.date: 03/24/2023
5+
ms.date: 07/11/2023
66
online version: https://learn.microsoft.com/powershell/module/psreadline/set-psreadlineoption?view=powershell-5.1&WT.mc_id=ps-gethelp
77
schema: 2.0.0
88
title: Set-PSReadLineOption
@@ -32,6 +32,11 @@ Set-PSReadLineOption [-EditMode <EditMode>] [-ContinuationPrompt <String>] [-His
3232
The `Set-PSReadLineOption` cmdlet customizes the behavior of the **PSReadLine** module when you're
3333
editing the command line. To view the **PSReadLine** settings, use `Get-PSReadLineOption`.
3434

35+
The options set by this command only apply to the current session. To persist any options, add them
36+
to a profile script. For more information, see
37+
[about_Profiles](../Microsoft.PowerShell.Core/About/about_Profiles.md) and
38+
[Customizing your shell environment](/powershell/scripting/learn/shell/creating-profiles).
39+
3540
## EXAMPLES
3641

3742
### Example 1: Set foreground and background colors
@@ -141,6 +146,29 @@ block object.
141146
For more information, see
142147
[about_Providers](/powershell/module/microsoft.powershell.core/about/about_providers).
143148

149+
### Example 7: Use HistoryHandler to filter commands added to history
150+
151+
The following example shows how to use the `AddToHistoryHandler` to prevent saving any git commands
152+
to history.
153+
154+
```powershell
155+
$ScriptBlock = {
156+
Param([string]$line)
157+
158+
if ($line -match "^git") {
159+
return $false
160+
} else {
161+
return $true
162+
}
163+
}
164+
165+
Set-PSReadLineOption -AddToHistoryHandler $ScriptBlock
166+
```
167+
168+
The scriptblock returns `$false` if the command started with `git`. This has the same effect as
169+
returning the `SkipAdding` **AddToHistory** enum. If the command doesn't start with `git`, the
170+
handler returns `$true` and PSReadLine saves the command in history.
171+
144172
## PARAMETERS
145173

146174
### -AddToHistoryHandler

reference/7.2/PSReadLine/Set-PSReadLineOption.md

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
external help file: Microsoft.PowerShell.PSReadLine2.dll-Help.xml
33
Locale: en-US
44
Module Name: PSReadLine
5-
ms.date: 03/24/2023
5+
ms.date: 07/11/2023
66
online version: https://learn.microsoft.com/powershell/module/psreadline/set-psreadlineoption?view=powershell-7.2&WT.mc_id=ps-gethelp
77
schema: 2.0.0
88
title: Set-PSReadLineOption
@@ -33,6 +33,11 @@ Set-PSReadLineOption [-EditMode <EditMode>] [-ContinuationPrompt <String>] [-His
3333
The `Set-PSReadLineOption` cmdlet customizes the behavior of the **PSReadLine** module when you're
3434
editing the command line. To view the **PSReadLine** settings, use `Get-PSReadLineOption`.
3535

36+
The options set by this command only apply to the current session. To persist any options, add them
37+
to a profile script. For more information, see
38+
[about_Profiles](../Microsoft.PowerShell.Core/About/about_Profiles.md) and
39+
[Customizing your shell environment](/powershell/scripting/learn/shell/creating-profiles).
40+
3641
## EXAMPLES
3742

3843
### Example 1: Set foreground and background colors
@@ -142,6 +147,29 @@ block object.
142147
For more information, see
143148
[about_Providers](/powershell/module/microsoft.powershell.core/about/about_providers).
144149

150+
### Example 7: Use HistoryHandler to filter commands added to history
151+
152+
The following example shows how to use the `AddToHistoryHandler` to prevent saving any git commands
153+
to history.
154+
155+
```powershell
156+
$ScriptBlock = {
157+
Param([string]$line)
158+
159+
if ($line -match "^git") {
160+
return $false
161+
} else {
162+
return $true
163+
}
164+
}
165+
166+
Set-PSReadLineOption -AddToHistoryHandler $ScriptBlock
167+
```
168+
169+
The scriptblock returns `$false` if the command started with `git`. This has the same effect as
170+
returning the `SkipAdding` **AddToHistory** enum. If the command doesn't start with `git`, the
171+
handler returns `$true` and PSReadLine saves the command in history.
172+
145173
## PARAMETERS
146174

147175
### -AddToHistoryHandler

reference/7.3/PSReadLine/Set-PSReadLineOption.md

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
external help file: Microsoft.PowerShell.PSReadLine2.dll-Help.xml
33
Locale: en-US
44
Module Name: PSReadLine
5-
ms.date: 03/24/2023
5+
ms.date: 07/11/2023
66
online version: https://learn.microsoft.com/powershell/module/psreadline/set-psreadlineoption?view=powershell-7.3&WT.mc_id=ps-gethelp
77
schema: 2.0.0
88
title: Set-PSReadLineOption
@@ -33,6 +33,11 @@ Set-PSReadLineOption [-EditMode <EditMode>] [-ContinuationPrompt <String>] [-His
3333
The `Set-PSReadLineOption` cmdlet customizes the behavior of the **PSReadLine** module when you're
3434
editing the command line. To view the **PSReadLine** settings, use `Get-PSReadLineOption`.
3535

36+
The options set by this command only apply to the current session. To persist any options, add them
37+
to a profile script. For more information, see
38+
[about_Profiles](../Microsoft.PowerShell.Core/About/about_Profiles.md) and
39+
[Customizing your shell environment](/powershell/scripting/learn/shell/creating-profiles).
40+
3641
## EXAMPLES
3742

3843
### Example 1: Set foreground and background colors
@@ -142,6 +147,29 @@ block object.
142147
For more information, see
143148
[about_Providers](/powershell/module/microsoft.powershell.core/about/about_providers).
144149

150+
### Example 7: Use HistoryHandler to filter commands added to history
151+
152+
The following example shows how to use the `AddToHistoryHandler` to prevent saving any git commands
153+
to history.
154+
155+
```powershell
156+
$ScriptBlock = {
157+
Param([string]$line)
158+
159+
if ($line -match "^git") {
160+
return $false
161+
} else {
162+
return $true
163+
}
164+
}
165+
166+
Set-PSReadLineOption -AddToHistoryHandler $ScriptBlock
167+
```
168+
169+
The scriptblock returns `$false` if the command started with `git`. This has the same effect as
170+
returning the `SkipAdding` **AddToHistory** enum. If the command doesn't start with `git`, the
171+
handler returns `$true` and PSReadLine saves the command in history.
172+
145173
## PARAMETERS
146174

147175
### -AddToHistoryHandler

reference/7.4/PSReadLine/Set-PSReadLineOption.md

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
external help file: Microsoft.PowerShell.PSReadLine2.dll-Help.xml
33
Locale: en-US
44
Module Name: PSReadLine
5-
ms.date: 03/24/2023
5+
ms.date: 07/11/2023
66
online version: https://learn.microsoft.com/powershell/module/psreadline/set-psreadlineoption?view=powershell-7.4&WT.mc_id=ps-gethelp
77
schema: 2.0.0
88
title: Set-PSReadLineOption
@@ -33,6 +33,11 @@ Set-PSReadLineOption [-EditMode <EditMode>] [-ContinuationPrompt <String>] [-His
3333
The `Set-PSReadLineOption` cmdlet customizes the behavior of the **PSReadLine** module when you're
3434
editing the command line. To view the **PSReadLine** settings, use `Get-PSReadLineOption`.
3535

36+
The options set by this command only apply to the current session. To persist any options, add them
37+
to a profile script. For more information, see
38+
[about_Profiles](../Microsoft.PowerShell.Core/About/about_Profiles.md) and
39+
[Customizing your shell environment](/powershell/scripting/learn/shell/creating-profiles).
40+
3641
## EXAMPLES
3742

3843
### Example 1: Set foreground and background colors
@@ -142,6 +147,29 @@ block object.
142147
For more information, see
143148
[about_Providers](/powershell/module/microsoft.powershell.core/about/about_providers).
144149

150+
### Example 7: Use HistoryHandler to filter commands added to history
151+
152+
The following example shows how to use the `AddToHistoryHandler` to prevent saving any git commands
153+
to history.
154+
155+
```powershell
156+
$ScriptBlock = {
157+
Param([string]$line)
158+
159+
if ($line -match "^git") {
160+
return $false
161+
} else {
162+
return $true
163+
}
164+
}
165+
166+
Set-PSReadLineOption -AddToHistoryHandler $ScriptBlock
167+
```
168+
169+
The scriptblock returns `$false` if the command started with `git`. This has the same effect as
170+
returning the `SkipAdding` **AddToHistory** enum. If the command doesn't start with `git`, the
171+
handler returns `$true` and PSReadLine saves the command in history.
172+
145173
## PARAMETERS
146174

147175
### -AddToHistoryHandler

0 commit comments

Comments
 (0)