forked from ztrhgf/useful_powershell_functions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Quote-String.ps1
123 lines (89 loc) · 3.03 KB
/
Quote-String.ps1
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
function Quote-String {
<#
.SYNOPSIS
Function for splitting given text by delimiter and enclosing the resulting items into quotation marks.
.DESCRIPTION
Function for splitting given text by delimiter and enclosing the resulting items into quotation marks.
Input can be taken from pipeline, parameter or clipboard.
Result can be returned into console or clipboard. Can be returned joined (as string) or as array.
.PARAMETER string
Optional parameter.
String(s) that should be split and enclosed by quotation marks.
If none is provided, clipboard content is used.
.PARAMETER delimiter
Delimiter value.
Default is ','.
.PARAMETER joinUsing
String that will be used to join the resultant items.
Default is value in 'delimiter' parameter.
.PARAMETER outputToConsole
Switch for outputting result to the console instead of clipboard.
.PARAMETER dontJoin
Switch for omitting final join operation.
When 'outputToConsole' is used, you will get array.
When 'outputToConsole' is NOT used, clipboard will contain string with quoted item per line.
.PARAMETER quoteBy
String that will be used to enclose resultant items.
Default is '.
.EXAMPLE
Quote-String -string "ahoj, vole"
Result (saved into clipboard) will be: 'ahoj','vole'
.EXAMPLE
(clipboard contains "ahoj, vole")
Quote-String
Result (saved into clipboard) will be: 'ahoj','vole'
.EXAMPLE
Quote-String -string "ahoj, vole" -outputToConsole -joinUsing ";"
Result (in console) will be:
'ahoj';'vole'
.EXAMPLE
"ahoj", "vole" | Quote-String -outputToConsole -dontJoin
Result (in console) will be array containing:
'ahoj'
'vole'
#>
[CmdletBinding()]
[Alias("ConvertTo-QuotedString")]
param (
[Parameter(ValueFromPipeline = $true)]
[string[]] $string,
[string] $delimiter = ",",
[string] $joinUsing,
[switch] $outputToConsole,
[switch] $dontJoin,
[string] $quoteBy = "'"
)
if (!$joinUsing -and !$dontJoin) {
$joinUsing = $delimiter
}
# I need to take pipeline input as a whole (because of final save into clipboard)
if ($Input) {
Write-Verbose "Using automatic variable 'Input' content"
$string = $Input
}
if (!$string) {
Write-Verbose "Using clipboard content"
$string = Get-Clipboard -Raw
}
if (!$string) {
throw "'String' parameter and even clipboard are empty."
}
Write-Verbose "'String' parameter contains:`n$string"
$result = $string -split [regex]::escape($delimiter) | ? { $_ } | % {
$quoteBy + $_.trim() + $quoteBy
}
if ($outputToConsole) {
if ($joinUsing) {
$result -join $joinUsing
} else {
$result
}
} else {
Write-Warning "Result was copied to clipboard"
if ($joinUsing) {
Set-Clipboard ($result -join $joinUsing)
} else {
Set-Clipboard $result
}
}
}