网站首页 文章专栏 powershell使用技巧
记录我最近使用powershell的技巧。
比如Get-ChildItem
,使用Get-Help Get-ChildItem
或者加上参数-Online
查看在线文档Get-Help Get-ChildItem -Online
PS C:\Users\pengq\Desktop> Get-Help Get-ChildItem
名称
Get-ChildItem
语法
Get-ChildItem [[-Path] <string[]>] [[-Filter] <string>] [<CommonParameters>]
Get-ChildItem [[-Filter] <string>] [<CommonParameters>]
别名
gci
ls
dir
备注
Get-Help 在此计算机上找不到该 cmdlet 的帮助文件。它仅显示部分帮助。
-- 若要下载并安装包含此 cmdlet 的模块的帮助文件,请使用 Update-Help。
-- 若要联机查看此 cmdlet 的帮助主题,请键入: "Get-Help Get-ChildItem -Online" 或
转到 https://go.microsoft.com/fwlink/?LinkID=113308。
PS C:\Users\pengq\Desktop>
而且可以查到命令的别名,有linux风格的ls
,还有gci
、dir
声明变量可以使用$
,比如获取一个请求
$response = Invode-WebRequest "https://api.dpangzi.com/api/Code"
$reponse
$response.Content
PS C:\Users\pengq\Desktop> $response = Invoke-WebRequest "https://api.dpangzi.com/api/Code"
PS C:\Users\pengq\Desktop> $response
StatusCode : 200
StatusDescription : OK
Content : {"isRoot":true,"isDirectory":true,"directories":[{"name":"load_db","lastUpdateTime":"2022-04-16T15:
29:13.0024917+08:00","note":"Sqlite
DDL","currentPath":["load_db"]},{"name":"MySql","lastUpdateTime":...
RawContent : HTTP/1.1 200 OK
Transfer-Encoding: chunked
Connection: keep-alive
Vary: Accept-Encoding,Accept, Accept-Language, Accept-Encoding
Strict-Transport-Security: max-age=15552000
Access-Control-Expose-...
Forms : {}
Headers : {[Transfer-Encoding, chunked], [Connection, keep-alive], [Vary, Accept-Encoding,Accept,
Accept-Language, Accept-Encoding], [Strict-Transport-Security, max-age=15552000]...}
Images : {}
InputFields : {}
Links : {}
ParsedHtml : mshtml.HTMLDocumentClass
RawContentLength : 2724
PS C:\Users\pengq\Desktop> $response.Content
{"isRoot":true,"isDirectory":true,"directories":[{"name":"load_db","lastUpdateTime":"2022-04-16T15:29:13.0024917+08:00","note":"Sqlite DDL","currentPath":["load_db"]},{"name":"MySql","lastUpdateTime":"2022-04-16T15:29:13.0024917+08:00","note":"MySql DDL","currentPath":["MySql"]},{"name":"src","lastUpdateTime":"2022-04-16T15:29:14.0333072+08:00","note":"源码","currentPath":["src"]}],"files":[{"name":".gitattributes","lastUpdateTime":"2022-04-16T15:29:12.9867914+08:00","note":"git 路径属性","currentPath":[".gitattributes"]},{"name":".gitignore","lastUpdateTime":"2022-04-16T15:29:12.9867914+08:00","note":"git 忽略文件","currentPath":[".gitignore"]},{"name":"README.md","lastUpdateTime":"2022-04-16T15:32:54.2068545+08:00","note":"自述文件","currentPath":["README.md"]},{"name":"video-fragment.ps1","lastUpdateTime":"2022-04-16T15:29:14.0333072+08:00","note":"视频分片脚本","currentPath":["video-fragment.ps1"]}],"parentPaths":null,"readmeContent":"# dpz.core\r\n\r\n### 我的网站:\r\n\r\n| 地址 | 描述 |源码地址|\r\n| ---- | ---- |----|\r\n| https://core.dpangzi.com |主站|[Dpz.Core.Web](https://github.com/pengqian089/dpz.core/tree/master/src/Dpz.Core.Web)|\r\n| https://www.dpangzi.com |blazor web assembly 开发的站点|[Dpz.Core.WebMore](https://github.com/pengqian089/dpz.core/tree/master/src/Dpz.Core.WebMore)|\r\n| https://manage.dpangzi.com |后台管理系统|[Dpz.Core.Web.Dashboard](https://github.com/pengqian089/dpz.core/tree/master/src/Dpz.Core.Web.Dashboard)|\r\n| https://api.dpangzi.com/swagger/index.html |RestFul Web api|[Dpz.Core.WebApi](https://github.com/pengqian089/dpz.core/tree/master/src/Dpz.Core.WebApi)|\r\n| https://blog.dpangzi.com |博客|[hexo](https://github.com/pengqian089/hexo)|\r\n\r\n### 正在做和将要做的事:\r\n- [ ] 增加书签页面,并将菜单的图标替换为更合理的图标(优先)\r\n- [x] 重写Steam页面UI\r\n- [ ] 发表的文章自动同步至hexo博客\r\n- [x] 定时任务,自动更新源代码 **linux 服务器上同步源码有bug,待修复**\r\n- [ ] 主数据库定时把重要数据备份到本地\r\n- [ ] 访问统计图\r\n- [x] 源码相关说明数据迁移到主数 据库\r\n- [x] 移除网站相关的静态资源,全部转移到[静态仓库](https://github.com/pengqian089/SiteSource)中去,使用CDN加速\r\n- [x] WebApi完成源码管理,[主站](https://core.dpangzi.com/code)相关功能由REST API 完成\r\n- [x] webapi和主站使用同一套日志管理,并在后台可以查看\r\n- [x] 视频管理和DB关联\r\n- [x] 视频相关的弹幕不再使用ID分组,使用自定义名称以便方便管理","currentPaths":[],"codeContainer":null,"fileName":null,"type":0}
$str = "定义一串字符串"
#定义一组数组
$array = "aa","bb","cc",$str
$array
#result
aa
bb
cc
定义一串字符串
# 打印当前时间
[System.DateTime]::Now
PS C:\Users\pengq\Desktop> [System.DateTime]::Now
#result
Tuesday, April 19, 2022 9:16:56 PM
# 格式化当前时间
[System.DateTime]::Now.ToString("yyyy-MM-dd HH:mm:ss.fff")
#result
2022-04-19 21:18:11.436
调用C#类库的方式:[命名空间.类型]::属性或者方法
# 在数组中判断是否存在某一值
$array -contains "aa"
#result
True
# 是否相等
$array[0] -eq "aa"
#result
True
# 字符里面是否包含某些字符
$array[$array.Length - 1] -match "串"
#result
True
# 结合上面的比较方式,来筛选文件、文件夹
ls
#result
目录: C:\Users\pengq\Desktop
Mode LastWriteTime Length Name
---- ------------- ------ ----
d----- 4/13/2022 2:20 AM Dism++
d----- 4/6/2022 5:38 AM Temp
d----- 2/18/2022 11:00 AM 副屏文件
d----- 3/19/2022 2:19 AM 简历
-a---- 12/4/2021 1:39 AM 222 3DMark.url
-a---- 3/7/2022 7:31 PM 222 A Hat in Time.url
-a---- 12/8/2021 8:08 AM 2302 DbServer.rdp
-a---- 3/25/2022 10:01 PM 1170 document.txt
-a---- 3/8/2022 5:09 PM 2280 eDEX-UI.lnk
-a---- 12/9/2021 4:35 AM 222 FINAL FANTASY XV WINDOWS EDITION.url
-a---- 4/16/2022 3:02 PM 2382 GitHub Desktop.lnk
-a---- 1/29/2022 9:38 PM 84 github_token.txt
-a---- 11/17/2021 1:19 AM 1506 JetBrains Toolbox.lnk
-a---- 1/2/2022 8:25 PM 1242 MongoDB Compass Beta.lnk
-a---- 11/16/2021 5:52 PM 222 Monster Hunter World.url
-a---- 11/15/2021 5:14 PM 1180 Opera 浏览器.lnk
-a---- 3/31/2022 12:17 AM 2196 Postman.lnk
-a---- 11/13/2021 1:16 PM 222 Shadow of the Tomb Raider.url
-a---- 12/30/2021 3:41 AM 211 start-mongodb.ps1
-a---- 2/5/2022 4:08 PM 1296 to-720p.ps1
-a---- 1/1/2022 7:50 PM 222 Tom Clancy's Ghost Recon® Wildlands.url
-a---- 1/11/2022 8:47 PM 1401 video-fragment.ps1
-a---- 11/15/2021 7:09 PM 1413 Visual Studio Code.lnk
-a---- 11/13/2021 3:34 AM 222 Wallpaper Engine:壁纸引擎.url
-a---- 11/30/2021 12:29 AM 2344 WebServer.rdp
-a---- 3/19/2022 2:37 AM 1762 WebStorm.lnk
-a---- 4/13/2022 2:40 AM 260708281 windows10.0-kb5012592-x64_ea2cbcc90d772b5c41410e88f96e6cad1608f45b.ms
u
-a---- 3/16/2022 1:19 PM 222 Wizard of Legend.url
-a---- 3/7/2022 9:14 PM 21912962 下沙平面图.jpg
-a---- 3/12/2022 3:36 PM 223 双人成行.url
-a---- 2/28/2022 12:01 PM 1336 微信开发者工具.lnk
-a---- 7/20/2021 10:47 AM 849200 神秘力量V.exe
-a---- 2/18/2022 3:06 PM 223 赛博朋克 2077.url
# 筛选名称含有 git 的文件、文件夹
ls | where Name -match "git"
#result
目录: C:\Users\pengq\Desktop
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 4/16/2022 3:02 PM 2382 GitHub Desktop.lnk
-a---- 1/29/2022 9:38 PM 84 github_token.txt
# 筛选结果只显示名称
ls | where Name -match "git" | select Name
#result
Name
----
GitHub Desktop.lnk
github_token.txt
这里的筛选类似C#里面的linq
暂时就这些,以后再有再更新。