Process in Shell

本文汇总了PowerShell中常用的命令和技巧,包括进程管理、日期操作、数据结构使用、过滤器应用、文件权限更改等。此外还介绍了如何通过PowerShell进行软件安装与卸载、计算机重启、打印机管理等高级操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

$PID    #current process
Get-Process
Get-Process | Select-Object Name
Get-Process -processname "jm" | Select-Object *
Get-Process -processname "jm" | Select-Object WorkingSet64
Stop-Process -processname baiduyunguanjia   
stop-Process -Id 9556
kill -Id 9556

# help
get-help stop-process
Get-help kill
get-help Stop-Process -Examples
Get-Help kill -Online
Get-Process | Where-Object {$_.ProcessName.Contains("baiduyunguanjia") }
Get-Process | Where-Object 
# special notation
# http://blog.chinaunix.net/uid-9781829-id-1997782.html

# kill process
{$_.ProcessName.Contains("baiduyunguanjia") } | Stop-Process -CONFIRM
Get-Process | Where-Object {$_.ProcessName.Contains("baiduyunguanjia") } | Stop-Process -force

# date 
Get-Date
(Get-Date).DayOfWeek

# data structure 
Get-Member     # most important 
Get-Help -Name # get help information
Get-Command    # command
Get-Service | Format-table
            | Format-Custom
            | Format-Wide
            | Format-List 
Get-Service | Get-Member -MemberType Property
Get-Service | format-table name, servicetype, canshutdown            
# Filter
1,2,3,4 | Where-Object -FilterScript {$_ -lt 3}
Get-WmiObject -Class Win32_SystemDriver | Get-Member -MemberType Property
Get-WmiObject -Class Win32_SystemDriver | Where-Object -FilterScript {$_.State -eq "Running"}
Get-WmiObject -Class Win32_SystemDriver | Where-Object -FilterScript {$_.State -eq "Running"} | Where-Object -FilterScript {$_.StartMode -eq "Auto"}
Get-WmiObject -Class Win32_SystemDriver | Where-Object -FilterScript {$_.State -eq "Running"} | Where-Object -FilterScript {$_.StartMode -eq "Manual"} | Format-Table -Property Name,DisplayName
Get-ChildItem env:
# change file constrain
# http://www.cnblogs.com/binw/p/3921743.html
Get-Acl -Path <File or Folder Path> | Format-List
# get one file's right and give it to another
Get-Acl "W:\Test\test01.txt" | Set-Acl -Path "W:\Test\test02.txt"
# WinSCP does batch processing
# http://www.cnblogs.com/binw/p/4065607.html 
# a reference blog
# http://www.cnblogs.com/dreamer-fish/archive/2013/01/26/2878144.html
# some basic operations
# remove all files in a folder
Remove-Item C:\tobedeleted –Recurse
# reborn current computer
(Get-WmiObject -Class Win32_OperatingSystem -ComputerName .).Win32Shutdown(2)
# get all child item recururse
Get-ChildItem –Force c:\directory –Recurse
Get-WmiObject -Class Win32_ComputerSystem
Get-WmiObject -Class win32_BIOS -ComputerName .
Get-WmiObject -Class Win32_QuickFixEngineering -ComputerName .
# get current user
Get-WmiObject -Class Win32_ComputerSystem -Property UserName -ComputerName .
Get-WmiObject -Class Win32_Product -ComputerName . | Format-Wide -Column 1
# current IP address
Get-WmiObject -Class Win32_NetworkAdapterConfiguration -Filter IPEnabled=TRUE -ComputerName . | Format-Table -Property IPAddress
Get-WmiObject -Class Win32_NetworkAdapterConfiguration -Filter IPEnabled=TRUE -ComputerName . | Select-Object -Property [a-z]* -ExcludeProperty IPX*,WINS*
Get-WmiObject -Class win32_networkadapterconfiguration -Filter "DHCPEnabled = true" -ComputerName .
Get-WmiObject -Class Win32_NetworkAdapterConfiguration -Filter IPEnabled=true -ComputerName . | ForEach-Object -Process {$_.EnableDHCP()}
# software management
# equip MSI packet
(Get-WMIObject -ComputerName TARGETMACHINE -List | Where-Object -FilterScript {$_.Name -eq "Win32_Product"}).Install(\\MACHINEWHEREMSIRESIDES\path\package.msi)
# uplevel MSI
(Get-WmiObject -Class Win32_Product -ComputerName . -Filter "Name='name_of_app_to_be_upgraded'").Upgrade(\\MACHINEWHEREMSIRESIDES\path\upgrade_package.msi)
# remove current packet from current computer
(Get-WmiObject -Class Win32_Product -Filter "Name='product_to_remove'" -ComputerName . ).Uninstall()
# machine management
# shut down another computer after 60 seconds
Start-Sleep 60; Restart-Computer –Force –ComputerName TARGETMACHINE
# add printer
(New-Object -ComObject WScript.Network).AddWindowsPrinterConnection(\\printerserver\hplaser3)
# remove printer
(New-Object -ComObject WScript.Network).RemovePrinterConnection("\\printerserver\hplaser3 ")


EXE

> http://www.tuicool.com/articles/AbmAvq
>ttp://www.cnblogs.com/dudu/archive/2011/08/23/ProcessStartInfo_bat_powershell.html
>http://club.topsage.com/thread-2134637-1-1.html
Parameter:
> http://www.pstips.net/powershell-pass-args-to-scripts.html
>http://blog.vichamp.com/powershell/tip/2014/04/30/getting-arguments-from-command-line/
>http://www.pstips.net/question/4418.html
>http://www.jb51.net/article/49853.htm
>http://www.pstips.net/powershell-launching-applications.html
>http://www.jb51.net/article/53583.htm
>http://www.jb51.net/article/53065.htm
IP:
> http://www.pstips.net/get-current-ip.html
> http://www.pstips.net/get-all-assigned-ip-addresses.html
>http://www.cnblogs.com/dudu/archive/2011/08/23/ProcessStartInfo_bat_powershell.html
Create New Folder:
> http://blog.csdn.net/itanders/article/details/1731811
Process:
> http://stackoverflow.com/questions/651223/powershell-start-process-and-cmdline-switches
### Shell Scripting or Shell Commands Shell scripting is a fundamental aspect of Unix-like operating systems, including Linux and macOS. It involves writing scripts that automate repetitive tasks, manage system resources, and interact with other programs. The shell itself serves as both an interactive command interpreter and a scripting language[^1]. #### Special Meanings of Parentheses in Shell Scripts Parentheses `(` and `)` have specific meanings in shell scripting, particularly in bash. They are used for creating subshells, grouping commands, and defining arrays. For example: - **Subshells**: `(command1; command2)` creates a subshell where the commands inside the parentheses are executed in isolation from the main shell environment. - **Command Grouping**: `{ command1; command2; }` groups commands together but does not create a subshell. Note the semicolon after each command and the braces. - **Array Definitions**: `array=(value1 value2 value3)` defines an array named `array` with three elements. When using parentheses literally in commands like `tcpdump`, they must be escaped with a backslash (`\`) so the shell interprets them as literal characters rather than part of its syntax[^1]. ```bash tcpdump \( host 192.168.1.1 \) ``` #### External Programs in Shell Scripts Many useful commands within shell scripts are external Unix utilities rather than built-in shell commands. Some common external utilities include: - `tr`: Translating or squeezing characters. - `grep`: Searching files for lines that match patterns. - `expr`: Evaluating expressions. - `cut`: Removing sections from each line of files. Built-in commands such as `echo`, `test`, and `which` are often directly integrated into the shell, providing faster execution without invoking an external process[^2]. #### Identifying Directories and Executables The `ls` command can help identify directories and executable programs. By using the `-F` flag, `ls` appends a `/` to directory names and a `*` to executable program names. This feature aids in quickly distinguishing between different types of files when navigating or scripting in the shell[^3]. ```bash ls -F /path/to/directory ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值