PowerShell-自定义的配置文件
PowerShell 5.1
一般Windows 10自带的是这个版本的PowerShell,这个版本的自定义配置文件的文件编码要保存为ANSI才行。
PowerShell 7
这个是通过github另外下载的,这个版本的自定义配置文件的文件编码要保存为utf-8才行。
配置文件代码
其实也没啥,主要加了一个时间显示和我可能用到的命令
1 #↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓定义的变量↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓ 2 3 #↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑定义的变量↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑ 4 5 #**************************************定义的函数******************************************* 6 #定义命令提示符提示的内容 7 Function Prompt() 8 { 9 switch ($PSPromptMode) 10 { 11 'Cmd' 12 { 13 "$($executionContext.SessionState.Path.CurrentLocation)$('>' * ($nestedPromptLevel + 1)) " 14 } 15 'Arrow' 16 { 17 '> ' 18 } 19 'None' 20 { 21 "PS $((Get-Date).DateTime) $($executionContext.SessionState.Path.CurrentLocation)$('>' * ($nestedPromptLevel + 1)) " 22 } 23 'Simple' 24 { 25 'PS> ' 26 } 27 Default 28 { 29 "PS $($executionContext.SessionState.Path.CurrentLocation)$('>' * ($nestedPromptLevel + 1)) " 30 } 31 } 32 } 33 34 <# 35 .Synopsis 36 设置控制台提示风格 37 .DESCRIPTION 38 设置控制台提示风格,支持五种风格:Normal,Cmd,Arrow,Simple,None 39 #> 40 Function Set-Prompt 41 { 42 param( 43 [Parameter(Mandatory=$true)] 44 [ValidateSet('Normal','Cmd','Arrow','Simple', 'None',IgnoreCase = $true)] 45 $Mode 46 ) 47 $varPSPromptMode = (Get-Variable 'PSPromptMode' -ErrorAction SilentlyContinue) 48 #配置变量不存在则新建 49 if ( $varPSPromptMode -eq $null) 50 { 51 New-Variable -Name 'PSPromptMode' -Value $Mode -Scope 'Global' 52 $varPSPromptMode = Get-Variable -Name 'PSPromptMode' 53 #$varPSPromptMode.Description = '提示函数配置变量' 54 55 #限制配置变量的取值集合 56 $varPSPromptModeAtt = New-Object System.Management.Automation.ValidateSetAttribute('Normal','Cmd','Arrow','Simple','None') 57 #$varPSPromptMode.Attributes.Add($varPSPromptModeAtt) 58 59 #限制配置变量为只读并且可以贯穿所有作用域ls 60 #$varPSPromptMode.Options = 'ReadOnly, AllScope' 61 } 62 #更新配置 63 #只读变量可以通过-force选项更新值 64 Set-Variable -Name PSPromptMode -Value $Mode -Force 65 } 66 67 Function get-SystemStartTime(){ 68 <# 69 .SYNOPSIS 70 显示Windows系统启动的时间以及自上次启动以来,运行了多长时间 71 .DESCRIPTION 72 本函数无参数,直接运行即可 73 .example 74 get-SystemStartTime() 75 76 #> 77 Set-Variable -Description "记录系统启动时间" -Name "system_start_time" -Scope "private" -Value ((Get-CimInstance Win32_OperatingSystem).LastBootUpTime) 78 $时间段 = New-TimeSpan -Start (Get-Variable -Name "system_start_time").Value -End (Get-Date) 79 $运行时间 = '' 80 if($时间段.Days -ge 365){ 81 #计算年,大于等于365 82 $运行时间 += ([int][Math]::Floor($时间段.Days / 365)).ToString()+"年" 83 $时间段.Days = $时间段.Days%365 84 } 85 if($时间段.Days -lt 365 -and $时间段.Days -gt 0){ 86 #计算天,0-365天 87 $运行时间 += $时间段.Days.ToString() + "天" 88 } 89 $运行时间 += $时间段.Hours.ToString() + "时" + $时间段.Minutes.ToString() + "分" + $时间段.Seconds.ToString() + "秒" 90 Write-Host -Object $运行时间 91 } 92 93 94 Function Format-ByteSize{ 95 <# 96 .SYNOPSIS 97 将数值转换为表示数字的字符串,该数字表示为以字节、千字节、兆字节或千兆字节为单位的大小值,具体取决于大小。 98 输入值:以字节大小表示的值 99 返回值:结果的字符串形式;若是转换失败,则原样返回$Size参数值 100 .example 101 将1234友好显示 102 > Format-ByteSize -Size 1234 103 1.20 KB 104 105 .example 106 使得输出结果单行显示 107 Format-ByteSize -Size 1234 | Write-Host -NoNewline 108 1.20 KB 109 #> 110 param( 111 [Parameter(Mandatory = $true, ValueFromPipeline = $true)] 112 [double]$SizeInBytes 113 ) 114 switch ($SizeInBytes) { 115 {$_ -ge 1PB} {"{0:N2} PB" -f ($SizeInBytes / 1PB); break} 116 {$_ -ge 1TB} {"{0:N2} TB" -f ($SizeInBytes / 1TB); break} 117 {$_ -ge 1GB} {"{0:N2} GB" -f ($SizeInBytes / 1GB); break} 118 {$_ -ge 1MB} {"{0:N2} MB" -f ($SizeInBytes / 1MB); break} 119 {$_ -ge 1KB} {"{0:N2} KB" -f ($SizeInBytes / 1KB); break} 120 default {"$SizeInBytes Bytes"} 121 } 122 } 123 124 Function du{ 125 <# 126 .SYNOPSIS 127 类似于Linux的du命令; 128 .description 129 直接运行该命令将获取当前路径下的所有文件大小和该目录的大小 130 .example 131 获取当前所在路径的大小 132 du 133 .example 134 获取指定文件夹的大小 135 du 136 #> 137 138 param 139 ( 140 [Parameter(Position=0, 141 HelpMessage="要检查的路径,若是文件,将只输出该文件的大小;若是目录,则 输出该目录以及该目录下的一级文件大小(一级目录则只输出目录本身的大小)",ValueFromPipeline)] 142 [System.String]$LiteralPath = '.' 143 ) 144 if(Test-Path -LiteralPath $LiteralPath){ 145 $LiteralPath = (Get-Item -LiteralPath $LiteralPath).FullName 146 #目录存在 147 Write-Verbose "目录存在。" 148 $file_type = (Get-Item -LiteralPath $LiteralPath).Attributes 149 if("Directory" -eq $file_type.Name){ 150 #目录 151 Write-Verbose "统计计算目录占用空间大小。" 152 "$LiteralPath 占用空间为:" + (Format-ByteSize((Get-ChildItem -LiteralPath $LiteralPath -Force -Recurse 2>$null |Measure-Object -Property Length -Sum).Sum).ToString()) 153 Write-Verbose "输出$LiteralPath 目录下的子文件(目录)大小" 154 Get-ChildItem -LiteralPath $LiteralPath -Force | Format-Table -Property Name,@{Name="大小";Expression={Format-ByteSize($_.Length)};Alignment="Right"} 155 } 156 else{ 157 #其他,这里做冗余处理,本质上跟文件的处理方式一样 158 Write-Verbose "输出$LiteralPath 目录下的子文件(目录)大小" 159 Get-ChildItem -LiteralPath $LiteralPath -Force | Format-Table -Property Name,@{Name="大小";Expression={Format-ByteSize($_.Length)};Alignment="Right"} 160 } 161 } 162 else{ 163 Write-Host -Object "$LiteralPath 不存在!" -ForegroundColor Red -BackgroundColor Black 164 } 165 } 166 Function Find-File(){ 167 <# 168 .SYNOPSIS 169 在指定的目录下查询指定的文件; 170 .description 171 如果不指定目录,则认为是在当前目录下查询;始终递归查询。 172 .parameter.Path 173 要查询的根路径,默认为当前目录 174 .parameter.Name 175 一般理解:要查询的文件名称,一般情况下用该参数去匹配文件名称 176 准确理解:用来筛选的字符串值,它可以配合filter_attribute_value参数做属性值筛选,当作为此种情况时,本参数值就是用来筛选的值 177 .parameter.attribute_value 178 最终需要的属性值,不指定则返回整个对象。 179 目前只支持从以下范围内取值:Attributes,CreationTime,CreationTimeUtc,Directory,DirectoryName,Exists,Extension,FullName,IsReadOnly,LastAccessTime,LastAccessTimeUtc,LastWriteTime,LastWriteTimeUtc,Length,Name 180 .parameter.fuzzy_match 181 开关参数;表示是否开启模糊匹配,即对Name参数使用通配符,使其在内部具体表现为*Name*。\n默认为`$false 182 .parameter.filter_attribute_value 183 筛选属性值,指定以什么样的文件对象属性来筛选,默认是使用文件对象的name属性去筛选。 184 目前只支持从以下范围内取值:Attributes,CreationTime,CreationTimeUtc,Directory,DirectoryName,Exists,Extension,FullName,IsReadOnly,LastAccessTime,LastAccessTimeUtc,LastWriteTime,LastWriteTimeUtc,Length,Name 185 .example 186 查询当前目录下的temp.txt文件 187 Find_file -name "temp.txt" 188 .example 189 查询当前目录下的以txt为后缀的文件 190 Find_file -name "*.txt" 191 或者 192 Find-File -Name ".html" -fuzzy_match 193 #注,使用-fuzzy_match参数时,内部实际上是Find-File -Name "*.html*";而不是Find-File -Name "*.html" 194 .example 195 查询$home目录下的以txt为后缀的文件 196 Find_file -path $home -name "*.txt" 197 .example 198 查询当前目录下,文件路径存在test目录的文件 199 Find-File -filter_attribute_value FullName -Name "\test\" -fuzzy_match -attribute_value FullName|Select-Object -First 5 200 FullName 201 -------- 202 D:\temp\ch02\2.1\test\Debug 203 D:\temp\ch02\2.1\test\test 204 D:\temp\ch02\2.1\test\test.sln 205 206 #> 207 param 208 ( 209 [Parameter(Position=0, 210 HelpMessage="要查询的根路径",ValueFromPipeline)] 211 [System.String]$Path = '.' 212 ,[Parameter(Position=1,Mandatory, 213 HelpMessage = "要查询的文件名称。")] 214 [System.String]$Name 215 ,[Parameter( 216 HelpMessage = "最终需要的属性值,不指定则返回整个对象。目前只支持从以下范围内取值:Attributes,CreationTime,CreationTimeUtc,Directory,DirectoryName,Exists,Extension,FullName,IsReadOnly,LastAccessTime,LastAccessTimeUtc,LastWriteTime,LastWriteTimeUtc,Length,Name")] 217 [ValidateSet( "Attributes","CreationTime","CreationTimeUtc","Directory","DirectoryName","Exists","Extension","FullName","IsReadOnly","LastAccessTime","LastAccessTimeUtc","LastWriteTime","LastWriteTimeUtc","Length","Name")] 218 [System.String]$attribute_value 219 ,[Parameter(HelpMessage = "开关参数;表示是否开启模糊匹配,即对Name参数使用通配符,使其在内部具体表现为*Name*。\n默认为`$false")] 220 [switch] 221 [System.Management.Automation.SwitchParameter]$fuzzy_match 222 ,[Parameter( 223 HelpMessage = "筛选属性值,指定以什么样的文件对象属性来筛选,默认是使用文件对象的name属性去筛选。:Attributes,CreationTime,CreationTimeUtc,Directory,DirectoryName,Exists,Extension,FullName,IsReadOnly,LastAccessTime,LastAccessTimeUtc,LastWriteTime,LastWriteTimeUtc,Length,Name")] 224 [ValidateSet( "Attributes","CreationTime","CreationTimeUtc","Directory","DirectoryName","Exists","Extension","FullName","IsReadOnly","LastAccessTime","LastAccessTimeUtc","LastWriteTime","LastWriteTimeUtc","Length","Name")] 225 [System.String]$filter_attribute_value = "Name" 226 ) 227 if(Test-Path -Path $Path){ 228 if($fuzzy_match){ 229 $Name = "*" + $Name + "*" 230 } 231 if($filter_attribute_value -eq "FullName" -and $Name -like "*/*"){ 232 Write-Host "请注意,Name参数值中存在正斜杠(/),但是Windows系统中的路径中是使用反斜杠(\),此处已经将正斜杠替换为反斜杠。" -ForegroundColor Yellow 233 $Name = $Name -replace "/" ,"\" 234 } 235 if($attribute_value.Length -eq 0){ 236 Get-ChildItem -Path $Path -Recurse | Where-Object -FilterScript {$_.$filter_attribute_value -like $Name} 237 } 238 else{ 239 Get-ChildItem -Path $Path -Recurse | Where-Object -FilterScript {$_.$filter_attribute_value -like $Name}|Select-Object -Property $attribute_value|Format-Table -AutoSize -Wrap 240 } 241 } 242 else{ 243 Write-Host -Object "$Path 不是有效路径!" -ForegroundColor Red -BackgroundColor Black 244 } 245 } 246 function convert-urldecoding{ 247 <# 248 .SYNOPSIS 249 解码url字符,使其转换为可直接阅读的形式。 250 .DESCRIPTION 251 url中若是包含Unicode字符的话,就会采用百分号编码的形式,如:java/%E6%96%87%E6%A1%A3/%E7%BF%BB%E8%AF%91/api/java.base/java/io/PrintWriter.html#println(java.lang.String) 252 本函数将其转换可以直接阅读的形式:file:///D:/工作/temp/2023年4月19日/ArithmeticException.html 253 254 UTF-8解码规则: 255 256 规则1:对于单字节的字符,8个bit位中高位必须等于0,这完全等同于127位最初的ASCII码。比如大写字母A,对应的二进制为01000001。 257 258 规则2:对于需要n个字节的字符 259 260 - 第一个字节: 261 - 高位前n位为`1` 262 - 第n+1位为`0` 263 - 其他字节: 264 - 高位以`10`开头 265 - 未提及的位使用对应的unicode补充,不足的在高位用0补充 266 267 二进制表示 说明 268 0xxxxxxx 完全兼容ASCII 269 110xxxxx 10xxxxxx 110表示需要两个字节(当前字节和随后的一个字节)表示当前字符 270 1110xxxx 10xxxxxx 10xxxxxx 1110表示需要3个字节,其余同上 271 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx 表示需要4个字节 272 273 UTF-8 百分号编码规则:%后面跟2位16进制数字 274 .parameter source_str 275 source_str:源字符串,也就是需要解码的url字符串。 276 .example 277 convert-urldecoding -source_str "file:///D:/%E5%B7%A5%E4%BD%9C/temp/2023%E5%B9%B44%E6%9C%8819%E6%97%A5/ArithmeticException.html" 278 #输出 279 file:///D:/工作/temp/2023年4月19日/ArithmeticException.html 280 #> 281 param 282 ( 283 [Parameter(Mandatory)] 284 [string]$source_str 285 ) 286 287 #$source_str = "file:///D:/java/%E6%96%87%E6%A1%A3/%E7%BF%BB%E8%AF%91/api/java.base/java/io/PrintWriter.html#println(java.lang.String)" 288 #$source_str = “file:///D:/%E5%B7%A5%E4%BD%9C/temp/2023%E5%B9%B44%E6%9C%8819%E6%97%A5/ArithmeticException.html” 289 290 [regex]$percent_char_regex = "(%[0-9A-F]{2})+" 291 292 $source_str_list = $percent_char_regex.Matches($source_str) 293 #$source_str_list 294 [System.Collections.ArrayList]$result_str_list = New-Object -TypeName "System.Collections.ArrayList" 295 296 $source_str_list|ForEach-Object{ 297 $utf8_code = $_.Value.Split('%'); 298 $result_str = "" 299 for($index =1 ;$index -lt $utf8_code.count;){ 300 #这里要从1开始,因为源字符串第一个字符就是分隔符 301 #几个字节 302 $char_num=0; 303 $temp_hex = "0x".Insert(2, $utf8_code[$index]); 304 $code_int = [int]$temp_hex 305 if(($code_int -bor 192) -eq $code_int){ 306 #192 ==> 1100 0000 307 #至少2个字节长度 308 $char_num=2; 309 if(($code_int -bor 224) -eq $code_int){ 310 #224 ==> 1110 0000 311 #至少3个字节长度 312 $char_num=3; 313 if(($code_int -bor 240) -eq $code_int){ 314 #240 ==> 1111 0000 315 #4个字节长度 316 $char_num=4; 317 } 318 } 319 } 320 else{ 321 #说明就是ASCII本身,一个字节 322 $char_num=1; 323 } 324 #"字节长度:" + $char_num 325 326 #将字符组合 327 [System.Collections.ArrayList]$bytes=@(); 328 for($j_index=0;$j_index -lt $char_num;$j_index++){ 329 #"下标:" + ($index+$j_index) 330 $number = [byte]("0x".Insert(2, $utf8_code[$index+$j_index])) 331 $bytes.Add($number) > $null 332 } 333 $index += $char_num 334 $utf8 = New-Object System.Text.UTF8Encoding 335 $result_str += $utf8.GetString($bytes,0,$char_num); 336 } 337 $result_str_list.Add($result_str) > $null 338 } 339 if($result_str_list.Count -eq $source_str_list.Count){ 340 $result_str = $source_str; 341 for($index = 0; $index -lt $source_str_list.Count;$index++){ 342 $result_str = $result_str.Replace($source_str_list[$index].Value,$result_str_list[$index]); 343 } 344 $result_str 345 }else{ 346 #出毛病了,放弃结果 347 Write-Host -Object "需要解析的字符数组长度和解析结果数组长度不对等,解析转换失败,请关注脚本运行中的错误提示。" -ForegroundColor Red -BackgroundColor Black; 348 } 349 } 350 #--------------------------------定义的别名------------------------------------------------ 351 Set-Alias -Name uptime -Value get-SystemStartTime 352 Set-Alias -Name urldecod -Value convert-urldecoding 353 #↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑定义的别名↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑ 354 355 #*******************************************执行函数************************ 356 Set-Prompt -Mode None 357 #*******************************************执行命令************************ 358 cd $home
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
2019-05-03 组建双路x79主机
2018-05-03 c++复习:C++输入和输出流