-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathStart-WT.ps1
164 lines (148 loc) · 4.99 KB
/
Start-WT.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
function Start-WT
{
<#
.Synopsis
Starts a new instance of Windows Terminal
.Description
Starts a new instance of Windows Terminal, and sends commands to it.
.Link
Get-WTProfile
.Example
Start-WT
.Example
Start-WT -Profile PowerShell
.Example
Start-WT -Profile 'Windows PowerShell' -CommandLine 'powershell -nologo'
#>
[OutputType([Nullable],[Diagnostics.Process])]
[Diagnostics.CodeAnalysis.SuppressMessageAttribute(
"PSUseShouldProcessForStateChangingFunctions", "", Justification="Not changing state"
)]
param(
# The name of the Windows Terminal Tab profile
[Parameter(ValueFromPipelineByPropertyName)]
[Alias('Profile')]
[string]
$ProfileName,
# The working directory used by Windows Terminal
[Parameter(ValueFromPipelineByPropertyName)]
[string]
$WorkingDirectory,
# The command line run by Windows Terminal
[Parameter(ValueFromPipelineByPropertyName)]
[string]
$CommandLine,
# If set, will split the pane.
[Parameter(ValueFromPipelineByPropertyName)]
[switch]
$Split,
# If provided, will split the pane horizontally or veritcally
[Parameter(ValueFromPipelineByPropertyName)]
[ValidateSet('Horizontal','H', 'Vertical','V')]
[string]
$SplitDirection,
# If provided, will focus a given tab index.
[Parameter(ValueFromPipelineByPropertyName)]
[int]
$FocusTabIndex,
# If provided, will focus the next tab.
[Parameter(ValueFromPipelineByPropertyName)]
[Alias('Next')]
[switch]
$FocusNextTab,
# If provided, will focus the previous tab.
[Parameter(ValueFromPipelineByPropertyName)]
[Alias('Previous')]
[switch]
$FocusPreviousTab,
# If set, will return the created process.
[switch]
$Passthru,
# If set, will start the process elevated
[Alias('RunAsAdministrator')]
[switch]
$Elevated
)
dynamicParam {
}
begin {
$allArgs = [Collections.Generic.List[Object]]::new()
}
process {
$allArgs.AddRange(
@(
#region New Tabs and Panes
if ($CommandLine -or $ProfileName -or $WorkingDirectory) {
if ($Split -or $SplitDirection) {
$(if ($allArgs.Count) {';'} else {''}) + $(
if ($allArgs.Count) {
if ($Split) {
'split-pane'
} elseif ('Horizontal','H' -contains $SplitDirection) {
'split-pane -H'
} elseif ('Vertical','V' -contains $SplitDirection) {
'split-pane -V'
}
} else {
'new-tab'
}
)
} else {
$(if ($allArgs.Count) {';'} else {''}) + 'new-tab'
}
if ($ProfileName) {
'--profile'
"`"$ProfileName`""
}
if ($WorkingDirectory) {
'--startingDirectory'
$WorkingDirectory
}
if ($CommandLine) {
$CommandLine -replace ';' ,'\;'
}
}
#endregion New Tabs and Panes
#region Tab Focus
elseif ($FocusNextTab -or $FocusPreviousTab -or $FocusTabIndex -ge 0)
{
';focus-tab'
if ($FocusTabIndex -ge 0) {
'--target'
$FocusTabIndex
} elseif ($FocusNextTab) {
'--next'
} elseif ($FocusPreviousTab) {
'--previous'
}
})
#endregion Tab Focus
)
}
end {
Write-Verbose "$allArgs"
if ($Elevated) # If we're going to run elevated,
{
# we actually need to start wt.exe from a process that is already elevated
# so we'll launch PowerShell, and pass the -command
$EmbeddedArguments = "@($(
foreach ($arg in $allArgs) {
$strArg = "$arg"
if ($strArg.Contains([Environment]::NewLine)) {
"@'
$($strArg + [Environment]::Newline)'@" + [Environment]::NewLine
} else {
"'$strArg';"
}
}
))"
$myPowerShell = Get-Process -Id $pid | Select-Object -ExpandProperty Path
if ($myPowerShell -like '*_ise*') {
$myPowerShell = Get-Command powershell | Select-Object -ExpandProperty Path
}
Start-Process -FilePath $myPowerShell -ArgumentList '-command', "Start-Process wt.exe -ArgumentList $EmbeddedArguments" -Verb Runas
} else {
Start-Process wt.exe -ArgumentList $allArgs -PassThru:$PassThru
}
}
}