-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGet-PSFavorite.ps1
More file actions
79 lines (69 loc) · 3 KB
/
Get-PSFavorite.ps1
File metadata and controls
79 lines (69 loc) · 3 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
69
70
71
72
73
74
75
76
77
78
79
<#
.SYNOPSIS
Get the PSFavorites list
.DESCRIPTION
Get the PSFavorites list. This will return the list of commands in the favorites list.
The favorites list is stored in the `AppData\Local\PSFavorite\Favorites.txt` file.
.EXAMPLE
Get-PSFavorite
Get the list of commands in the favorites list.
.EXAMPLE
Get-PSFavorite | Out-GridView
Get the list of commands in the favorites list and display it in a GridView.
.EXAMPLE
Get-PSFavorite -Property Command
Get only the commands in the favorites list.
.EXAMPLE
Get-PSFavorite -Filter "git"
Get the commands in the favorites list that contain the string "git" in either the command or the description.
.EXAMPLE
Get-PSFavorite -Property Description -Filter "git"
Get only the descriptions in the favorites list that contain the string "git".
.NOTES
The output of this cmdlet can be used with the `Remove-PSFavorite` cmdlet to remove items from the favorites list.
For example: `Get-PSFavorite -Filter "git" | Remove-PSFavorite` will remove all favorites that contain the string "git".
#>
function Get-PSFavorite {
[CmdletBinding()]
param(
# The property to select from the favorites list. This can be either "Command" or "Description".
[ValidateSet("Command", "Description", "All")]
[Alias("Select")]
[string] $Property = "All",
# Filter the favorites list by a string.
# This will return only the favorites that contain the given string in either the command or the description.
[Alias("Like", "Match", "Where")]
[string] $Filter,
# The path to the favorites list file.
[ValidateScript({ Test-Path -Path $_ -PathType Leaf })]
[Alias("Path", "Name", "FullName", "FullPath")]
[string] $FavoritesPath = $Script:FavoritesPath
)
# Get the favorites from the file
$Favorites = Get-Content -Path $FavoritesPath -Encoding UTF8
# Convert the favorites to a PSCustomObject
$Results = foreach ($Favorite in $Favorites) {
# C# `PSFavoritePredictor::ParseFavoriteLine` returns a `System.ValueTuple<string,string>`.
# PowerShell does not automatically deconstruct a `ValueTuple` into two variables,
# so we have to access Item1/Item2 explicitly.
$tuple = [PSFavorite.PSFavoritePredictor]::ParseFavoriteLine($Favorite)
$Command = $tuple.Item1
$Description = $tuple.Item2
# If the favorite lacks a description, use the command itself as its description
if ([string]::IsNullOrEmpty($Description)) {
$Description = $Command
}
# Output the favorite as a PSCustomObject
[PSCustomObject]@{
Command = $Command
Description = $Description
}
}
if ($Filter) {
$Results = $Results | Where-Object { $_.Command -like "*$Filter*" -or $_.Description -like "*$Filter*" }
}
if ($Property -and $Property -ne "All") {
return $Results | Select-Object -Property $Property
}
return $Results
}