PowerShell is a powerful scripting language and command line shell that is widely used to automate tasks, configuration management, and system management.
1. Service Management
1. View the service
- Get - Service: This command can list all services installed in the system, including service name, status (running, stopped, etc.), startup type (automatic, manual, disabled), etc.
- For example, Get - Service | Where - Object {$_.Status -eq "Running"} can filter out running services.
2. Start and stop the service
- Start - Service - Name "Spooler": Used to start a service named "Spooler" (Print spooler service).
- Stop - Service - Name "Spooler": This is used to stop the service.
2. Event log query
1. View event log
- Get - EventLog - LogName "Application": This command views records in the "Application" event log. You can replace "Application" with other log names as needed, such as "System" and "Security", etc.
- For example, Get - EventLog - LogName "System" - Newest 10 can view the latest 10 records in the system event log.
3. Variable operation
1. Define variables
- $myVar = "Hello, World": This defines a variable named myVar and assigns the string "Hello, World" to it.
2. View the value of the variable
- After defining the variable, enter the variable name directly to view its value. If you enter $myVar, "Hello, World" will be displayed.
4. Script execution related
1. Run the script
- If there is a script file named test.ps1, you can use .\test.ps1 to run it in Powershell (provided that the path to the script is already added to the scope allowed by the execution policy).
- If you encounter execution policy restrictions, you can first use Set - ExecutionPolicy - ExecutionPolicy RemoteSigned - Scope CurrentUser to set the execution policy of the current user (this is just a common temporary solution and you need to be cautious in production environments based on security policies).
5. Remote management (requires appropriate configuration)
1. Connect to a remote computer
- Enter - PSSession - ComputerName "RemoteComputer": This command allows you to connect to a remote computer named "RemoteComputer", and then you can execute Powershell commands on the remote computer, just like locally.
- Before making a remote connection, you need to make sure that the remote computer is configured to allow Powershell remote management and that network connections, permissions, etc. are correctly set.
6. Module management
1. View installed modules
- Get - Module: You can list the Powershell modules that have been installed in the current system.
2. Install the module
- For example, to install a module called "AzureRM" (a module for Azure Resource Management, for example only), you can use Install - Module - Name AzureRM (which may require administrator privileges and network connection to the module source).
This is all about this article about the complete collection of commonly used powershell commands. For more relevant contents of commonly used powershell commands, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!