Frequently Used Cmdlets and Shortcuts in Powershell.
1. List directory contents (ls, dir, gci):
```powershell
PS C:\\> Get-ChildItem
```
- `Get-ChildItem`: Lists the files and directories in the current directory.
- Common Aliases: `ls`, `dir`, `gci`.
- The cmdlet retrieves items from the current location or from the specified path. By default, it lists files and folders.
2. Copy a file (cp, copy, cpi):
```powershell
PS C:\\> Copy-Item src.txt dst.txt
```
- `Copy-Item`: Copies a file or directory from a source (`src.txt`) to a destination (`dst.txt`).
- Common Aliases: `cp`, `copy`, `cpi`.
- If the destination is a folder, the item will be copied into that folder. If the destination is a file, it will be copied and renamed accordingly.
3. Move a file (mv, move, mi):
```powershell
PS C:\\> Move-Item src.txt dst.txt
```
- `Move-Item`: Moves a file or directory from a source (`src.txt`) to a destination (`dst.txt`).
- Common Aliases: `mv`, `move`, `mi`.
- If the destination is a folder, the item is moved into that folder. If the destination is a file, it will be moved and renamed.
4. Search for text in a file:
```powershell
PS C:\\> Select-String –path c:\users\*.txt –pattern secret
PS C:\\> ls -r c:\users -file | % {Select-String -path $_ -pattern secret}
```
- `Select-String`: Searches for a text pattern in files.
- `–path`: Specifies the path to search. In this case, `c:\users\*.txt` specifies all `.txt` files in the `c:\users` directory.
- `–pattern`: Specifies the text pattern to search for, in this case, `secret`.
- The second command recursively searches through all `.txt` files in the `c:\users` directory using `ls -r` and then applies `Select-String` to each file.
5. Display file contents (cat, type, gc):
```powershell
PS C:\\> Get-Content file.txt
```
- `Get-Content`: Retrieves the contents of a file and displays them.
- Common Aliases: `cat`, `type`, `gc`.
- This cmdlet can be used to read the contents of text files or other types of files, outputting them to the console.
6. Show current directory (pwd, gl):
```powershell
PS C:\\> Get-Location
```
- `Get-Location`: Displays the current working directory (folder) where the user is located in the file system.
- Common Aliases: `pwd`, `gl`.
7. List running processes (ps, GPS):
```powershell
PS C:\\> Get-Process
```
- `Get-Process`: Lists all currently running processes on the system.
- Common Aliases: `ps`, `GPS`.
- The output shows information about each process, including the process name, ID, memory usage, and more.
8. List services:
```powershell
PS C:\\> Get-Service
```
- `Get-Service`: Retrieves a list of all services currently installed on the system.
- It includes the service's status (running, stopped, etc.), the name of the service, and its display name.
9. Format command output (Format-List):
```powershell
PS C:\\> ls | Format-List –property name
```
- `Format-List`: Formats the output of a command into a list format.
- `–property name`: Specifies which properties to display. In this case, only the `name` of the items in the directory will be shown.
- It’s useful for displaying data in a structured and readable format.
10. Paginate output:
```powershell
PS C:\\> ls –r | Out-Host -paging
```
- `Out-Host -paging`: Pauses the output and allows the user to scroll through it one page at a time.
- `-paging` is used to break up long output into pages, which is useful when working with large amounts of data.
11. Get SHA1 hash of a file:
```powershell
PS C:\\> Get-FileHash -Algorithm SHA1 file.txt
```
- `Get-FileHash`: Computes the hash value (checksum) for a file to ensure integrity or verify file authenticity.
- `-Algorithm SHA1`: Specifies that the SHA1 hashing algorithm should be used.
- `file.txt`: The path to the file for which the hash is being computed.
12. Export command output to CSV:
```powershell
PS C:\\> Get-Process | Export-Csv processes.csv
```
- `Export-Csv`: Exports the output of a command to a CSV (Comma-Separated Values) file.
- `Get-Process`: Retrieves the list of running processes.
- `processes.csv`: Specifies the output file where the data will be saved.
These cmdlets and their aliases are often used to perform basic file operations, retrieve system information, and format or export data.
Comments