2023-11-01 10:31:16 +08:00

63 lines
1.4 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

---
title: PowerShell
date: 2023-10-07 11:25:08
tags:
---
# 文件
## 新建文件
```powershell
New-Item <文件名>.<扩展名后缀>
New-Item my.ini
```
## 删除文件
```powershell
Remove-Item <文件名>.<扩展名后缀>
Remove-Item my.ini
```
## 对文件添加内容
```powershell
Set-Content <文件名>.<扩展名后缀> -value "<内容>"
Set-Content my.ini -value ""
```
## 新建文件夹
```powershell
New-Item data -ItemType Directory
```
# 做 sudo 命令
在Windows系统上sudo对应的就是管理员权限了。
一般使用Powershell时并不会管理员启动当执行需要权限的命令比如net start mysql就需要以管理员打开新的窗口。
为了一步到位这里给powershell创建一个`alias` -> `sudo` 来运行需要管理员权限的命令。
在文档目录中(在`powershell`执行`$profile`即可输出此文件路径),新建文件夹`WindowsPowerShell`,新建文件`Microsoft.PowerShell_profile.ps1`
> 此文件是在启动Powershell时执行的脚本。set-alias 在退出后就会失效,所以放到启动脚本中。
追加如下代码,**然后重启Powershell窗口。**
```text
function _sudo {
$ss = "$args ; pause"
Start-Process powershell -Verb runAs -ArgumentList $ss
}
set-alias -name sudo -value _sudo
```
保存后发现无法加载,因为默认不加载外部脚本,管理员权限下 powershell 运行:
```text
set-ExecutionPolicy RemoteSigned
```