From aed340476f79041e9d8d58e95da14a1c059b2e5b Mon Sep 17 00:00:00 2001 From: Fernando Crespo Date: Wed, 21 Dec 2022 01:26:27 -0300 Subject: [PATCH] windows/*: add page (#8783) * Add some PowerShell related commands * Include new pages for start-service, stop-service, set-service, show-markdown, resolve-path * Include new pages for measure-command.md, tee-object.md, get-acl.md, and set-acl.md * Include new pages for get-date, set-date, measure-object, sort-object, test-json, wait-process * Remove i18n identifier from all added pages * get-acl: Better wording and fix missing placeholder sls: Use kebab-case for tldr view original command * get-date: Better wording and change date string to ISO * test-json: Better wording, generic json, and add one more example * Add token {{}} to some missing places * Apply suggestions from code review Co-authored-by: Axel Navarro * Fix typo on pages/windows/resolve-path.md Co-authored-by: K.B.Dharun Krishna * Update pages/windows/measure-object.md Co-authored-by: Axel Navarro Co-authored-by: K.B.Dharun Krishna --- pages/windows/get-acl.md | 13 +++++++++++++ pages/windows/get-date.md | 21 +++++++++++++++++++++ pages/windows/measure-command.md | 13 +++++++++++++ pages/windows/measure-object.md | 13 +++++++++++++ pages/windows/out-string.md | 17 +++++++++++++++++ pages/windows/pwsh-where.md | 8 ++++++++ pages/windows/resolve-path.md | 17 +++++++++++++++++ pages/windows/select-string.md | 26 ++++++++++++++++++++++++++ pages/windows/set-acl.md | 13 +++++++++++++ pages/windows/set-date.md | 17 +++++++++++++++++ pages/windows/set-service.md | 17 +++++++++++++++++ pages/windows/show-markdown.md | 17 +++++++++++++++++ pages/windows/sls.md | 8 ++++++++ pages/windows/sort-object.md | 25 +++++++++++++++++++++++++ pages/windows/start-service.md | 17 +++++++++++++++++ pages/windows/stop-service.md | 17 +++++++++++++++++ pages/windows/tee-object.md | 13 +++++++++++++ pages/windows/test-json.md | 17 +++++++++++++++++ pages/windows/wait-process.md | 13 +++++++++++++ pages/windows/where-object.md | 17 +++++++++++++++++ 20 files changed, 319 insertions(+) create mode 100644 pages/windows/get-acl.md create mode 100644 pages/windows/get-date.md create mode 100644 pages/windows/measure-command.md create mode 100644 pages/windows/measure-object.md create mode 100644 pages/windows/out-string.md create mode 100644 pages/windows/pwsh-where.md create mode 100644 pages/windows/resolve-path.md create mode 100644 pages/windows/select-string.md create mode 100644 pages/windows/set-acl.md create mode 100644 pages/windows/set-date.md create mode 100644 pages/windows/set-service.md create mode 100644 pages/windows/show-markdown.md create mode 100644 pages/windows/sls.md create mode 100644 pages/windows/sort-object.md create mode 100644 pages/windows/start-service.md create mode 100644 pages/windows/stop-service.md create mode 100644 pages/windows/tee-object.md create mode 100644 pages/windows/test-json.md create mode 100644 pages/windows/wait-process.md create mode 100644 pages/windows/where-object.md diff --git a/pages/windows/get-acl.md b/pages/windows/get-acl.md new file mode 100644 index 000000000..682f9bbef --- /dev/null +++ b/pages/windows/get-acl.md @@ -0,0 +1,13 @@ +# Get-Acl + +> Gets the security descriptor for a resource, such as a file or registry key. +> This command can only be used through PowerShell. +> More information: . + +- Display the ACL for a specific directory: + +`Get-Acl {{path/to/directory}}` + +- Get an ACL for a registry key: + +`Get-Acl -Path {{HKLM:\System\CurrentControlSet\Control}} | Format-List` diff --git a/pages/windows/get-date.md b/pages/windows/get-date.md new file mode 100644 index 000000000..76f633580 --- /dev/null +++ b/pages/windows/get-date.md @@ -0,0 +1,21 @@ +# Get-Date + +> Gets the current date and time. +> This command can only be used through PowerShell. +> More information: . + +- Display the current date and time: + +`Get-Date` + +- Display the current date and time with a .NET format specifier: + +`Get-Date -Format "{{yyyy-MM-dd HH:mm:ss}}"` + +- Display the current date and time in UTC and ISO 8601 format: + +`(Get-Date).ToUniversalTime()` + +- Convert a Unix timestamp: + +`Get-Date -UnixTimeSeconds {{1577836800}}` diff --git a/pages/windows/measure-command.md b/pages/windows/measure-command.md new file mode 100644 index 000000000..03bfd5277 --- /dev/null +++ b/pages/windows/measure-command.md @@ -0,0 +1,13 @@ +# Measure-Command + +> Measures the time it takes to run script blocks and cmdlets. +> This command can only be used through PowerShell. +> More information: . + +- Measure the time it takes to run a command: + +`Measure-Command { {{command}} }` + +- Pipe input to Measure-Command (objects that are piped to `Measure-Command` are available to the script block that is passed to the Expression parameter): + +`10, 20, 50 | Measure-Command -Expression { for ($i=0; $i -lt $_; $i++) {$i} }` diff --git a/pages/windows/measure-object.md b/pages/windows/measure-object.md new file mode 100644 index 000000000..ad9624472 --- /dev/null +++ b/pages/windows/measure-object.md @@ -0,0 +1,13 @@ +# Measure-Object + +> Calculates the numeric properties of objects, and the characters, words, and lines in string objects, such as files of text. +> This command can only be used through PowerShell. +> More information: . + +- Count the files and folders in a directory: + +`Get-ChildItem | Measure-Object` + +- Pipe input to Measure-Command (objects that are piped to `Measure-Command` are available to the script block that is passed to the Expression parameter): + +`"One", "Two", "Three", "Four" | Set-Content -Path "{{path/to/file}}"; Get-Content "{{path/to/file}}"; | Measure-Object -Character -Line -Word` diff --git a/pages/windows/out-string.md b/pages/windows/out-string.md new file mode 100644 index 000000000..2a946af9c --- /dev/null +++ b/pages/windows/out-string.md @@ -0,0 +1,17 @@ +# Out-String + +> Outputs input objects as a string. +> This command can only be used through PowerShell. +> More information: . + +- Print host information as string: + +`Get-Alias | Out-String` + +- Convert each object to a string rather than concatenating all the objects into a single string: + +`Get-Alias | Out-String -Stream` + +- Use the `Width` parameter to prevent truncation: + +`@{TestKey = ('x' * 200)} | Out-String -Width {{250}}` diff --git a/pages/windows/pwsh-where.md b/pages/windows/pwsh-where.md new file mode 100644 index 000000000..afcaaf0a8 --- /dev/null +++ b/pages/windows/pwsh-where.md @@ -0,0 +1,8 @@ +# where + +> This command is an alias of `Where-Object`. +> More information: . + +- View documentation for the original command: + +`tldr Where-Object` diff --git a/pages/windows/resolve-path.md b/pages/windows/resolve-path.md new file mode 100644 index 000000000..a09f5c7a3 --- /dev/null +++ b/pages/windows/resolve-path.md @@ -0,0 +1,17 @@ +# Resolve-Path + +> Resolves the wildcard characters in a path, and displays the path contents. +> This command can only be used through PowerShell. +> More information: . + +- Resolve the home folder path: + +`Resolve-Path {{~}}` + +- Resolve a UNC path: + +`Resolve-Path -Path "\\{{hostname}}\{{path/to/file}}"` + +- Get relative paths: + +`Resolve-Path -Path {{path/to/file_or_directory}} -Relative` diff --git a/pages/windows/select-string.md b/pages/windows/select-string.md new file mode 100644 index 000000000..8138ec5bb --- /dev/null +++ b/pages/windows/select-string.md @@ -0,0 +1,26 @@ +# Select-String + +> Finds text in strings and files in PowerShell. +> This command can only be used through PowerShell. +> You can use `Select-String` similar to grep in UNIX or findstr.exe in Windows. +> More information: . + +- Search for a pattern within a file: + +`Select-String -Path "{{path/to/file}}" -Pattern '{{search_pattern}}'` + +- Search for an exact string (disables regular expressions): + +`Select-String -SimpleMatch "{{exact_string}}" {{path/to/file}}` + +- Search for pattern in all `.ext` files in current dir: + +`Select-String -Path "{{*.ext}}" -Pattern '{{search_pattern}}'` + +- Capture the specified number of lines before and after the line that matches the pattern: + +`Select-String --Context {{2,3}} "{{search_pattern}}" {{path/to/file}}` + +- Search stdin for lines that do not match a pattern: + +`Get-Content {{path/to/file}} | Select-String --NotMatch "{{search_pattern}}"` diff --git a/pages/windows/set-acl.md b/pages/windows/set-acl.md new file mode 100644 index 000000000..db7252eb6 --- /dev/null +++ b/pages/windows/set-acl.md @@ -0,0 +1,13 @@ +# Set-Acl + +> Changes the security descriptor of a specified item, such as a file or a registry key. +> This command can only be used through PowerShell. +> More information: . + +- Copy a security descriptor from one file to another: + +`$OriginAcl = Get-Acl -Path {{path/to/file}}; Set-Acl -Path {{path/to/file}} -AclObject $OriginAcl` + +- Use the pipeline operator to pass a descriptor: + +`Get-Acl -Path {{path/to/file}} | Set-Acl -Path {{path/to/file}}` diff --git a/pages/windows/set-date.md b/pages/windows/set-date.md new file mode 100644 index 000000000..787dfe779 --- /dev/null +++ b/pages/windows/set-date.md @@ -0,0 +1,17 @@ +# Set-Date + +> Changes the system time on the computer to a time that you specify. +> This command can only be used through PowerShell. +> More information: . + +- Add three days to the system date: + +`Set-Date -Date (Get-Date).AddDays({{3}})` + +- Set the system clock back 10 minutes: + +`Set-Date -Adjust -0:10:0 -DisplayHint Time` + +- Add 90 minutes to the system clock: + +`$90mins = New-TimeSpan -Minutes {{90}}; Set-Date -Adjust $90mins` diff --git a/pages/windows/set-service.md b/pages/windows/set-service.md new file mode 100644 index 000000000..1b594b45b --- /dev/null +++ b/pages/windows/set-service.md @@ -0,0 +1,17 @@ +# Set-Service + +> Starts, stops, and suspends a service, and changes its properties. +> This command can only be used through PowerShell. +> More information: . + +- Change a display name: + +`Set-Service -Name {{hostname}} -DisplayName "{{name}}"` + +- Change the startup type of services: + +`Set-Service -Name {{service_name}} -StartupType {{Automatic}}` + +- Change the description of a service: + +`Set-Service -Name {{service_name}} -Description "{{description}}"` diff --git a/pages/windows/show-markdown.md b/pages/windows/show-markdown.md new file mode 100644 index 000000000..2ab9c9f72 --- /dev/null +++ b/pages/windows/show-markdown.md @@ -0,0 +1,17 @@ +# Show-Markdown + +> Shows a Markdown file or string in the console in a friendly way using VT100 escape sequences or in a browser using HTML. +> This command can only be used through PowerShell. +> More information: . + +- Render markdown to console from a file: + +`Show-Markdown -Path {{path/to/file}}` + +- Render markdown to console from string: + +`{{"# Markdown content"}} | Show-Markdown` + +- Open Markdown file in a browser: + +`Show-Markdown -Path {{path/to/file}} -UseBrowser` diff --git a/pages/windows/sls.md b/pages/windows/sls.md new file mode 100644 index 000000000..78a9e42c3 --- /dev/null +++ b/pages/windows/sls.md @@ -0,0 +1,8 @@ +# sls + +> This command is an alias of `Select-String`. +> More information: . + +- View documentation for the original command: + +`tldr where-object` diff --git a/pages/windows/sort-object.md b/pages/windows/sort-object.md new file mode 100644 index 000000000..1b0d617ff --- /dev/null +++ b/pages/windows/sort-object.md @@ -0,0 +1,25 @@ +# Sort-Object + +> Sorts objects by property values. +> This command can only be used through PowerShell. +> More information: . + +- Sort the current directory by name: + +`Get-ChildItem | Sort-Object` + +- Sort the current directory by name descending: + +`Get-ChildItem | Sort-Object -Descending` + +- Sort items removing duplicates: + +`"a", "b", "a" | Sort-Object -Unique` + +- Sort the current directory by file length: + +`Get-ChildItem | Sort-Object -Property Length` + +- Sort processes with the highest memory usage based on their working set (WS) size: + +`Get-Process | Sort-Object -Property WS` diff --git a/pages/windows/start-service.md b/pages/windows/start-service.md new file mode 100644 index 000000000..ffbf25219 --- /dev/null +++ b/pages/windows/start-service.md @@ -0,0 +1,17 @@ +# Start-Service + +> Starts one or more stopped services. +> This command can only be used through PowerShell. +> More information: . + +- Start a service by using its name: + +`Start-Service -Name {{service_name}}` + +- Display information without starting a service: + +`Start-Service -DisplayName *{{name}}* -WhatIf` + +- Start a disabled service: + +`Set-Service {{service_name}} -StartupType {{manual}}; Start-Service {{service_name}}` diff --git a/pages/windows/stop-service.md b/pages/windows/stop-service.md new file mode 100644 index 000000000..dc1e616a0 --- /dev/null +++ b/pages/windows/stop-service.md @@ -0,0 +1,17 @@ +# Stop-Service + +> Stops one or more running services. +> This command can only be used through PowerShell. +> More information: . + +- Stop a service on the local computer: + +`Stop-Service -Name {{service_name}}` + +- Stop a service by using the display name: + +`Stop-Service -DisplayName "{{name}}"` + +- Stop a service that has dependent services: + +`Stop-Service -Name {{service_name}} -Force -Confirm` diff --git a/pages/windows/tee-object.md b/pages/windows/tee-object.md new file mode 100644 index 000000000..cca8da0ce --- /dev/null +++ b/pages/windows/tee-object.md @@ -0,0 +1,13 @@ +# Tee-Object + +> Saves command output in a file or variable and also sends it down the pipeline. +> This command can only be used through PowerShell. +> More information: . + +- Output processes to a file and to the console: + +`Get-Process | Tee-Object -FilePath {{path/to/file}}` + +- Output processes to a variable and `Select-Object`: + +`Get-Process notepad | Tee-Object -Variable {{proc}} | Select-Object processname,handles` diff --git a/pages/windows/test-json.md b/pages/windows/test-json.md new file mode 100644 index 000000000..7f9169918 --- /dev/null +++ b/pages/windows/test-json.md @@ -0,0 +1,17 @@ +# Test-Json + +> Tests whether a string is a valid JSON document. +> This command can only be used through PowerShell. +> More information: . + +- Test if a string from stdin is in JSON format: + +`'{{string}}' | Test-Json` + +- Test if a string JSON format: + +`Test-Json -Json '{{json_to_test}}'` + +- Test if a string from stdin matches a specific schema file: + +`'{{string}}' | Test-Json -SchemaFile {{path/to/schema.json}}` diff --git a/pages/windows/wait-process.md b/pages/windows/wait-process.md new file mode 100644 index 000000000..93e3cad43 --- /dev/null +++ b/pages/windows/wait-process.md @@ -0,0 +1,13 @@ +# Wait-Process + +> Waits for the processes to be stopped before accepting more input. +> This command can only be used through PowerShell. +> More information: . + +- Stop a process and wait: + +`Stop-Process -Id {{process_id}}; Wait-Process -Id {{process_id}}` + +- Wait for processes for a specified time: + +`Wait-Process -Name {{process_name}} -Timeout {{30}}` diff --git a/pages/windows/where-object.md b/pages/windows/where-object.md new file mode 100644 index 000000000..43cf3b208 --- /dev/null +++ b/pages/windows/where-object.md @@ -0,0 +1,17 @@ +# Where-Object + +> Selects objects from a collection based on their property values. +> This command can only be used through PowerShell. +> More information: . + +- Filter aliases by its name: + +`Get-Alias | Where-Object -{{Property}} {{Name}} -{{eq}} {{name}}` + +- Get a list of all services that are currently stopped. The `$_` automatic variable represents each object that is passed to the `Where-Object` cmdlet: + +`Get-Service | Where-Object {$_.Status -eq "Stopped"}` + +- Use multiple conditions: + +`Get-Module -ListAvailable | Where-Object { $_.Name -NotLike "Microsoft*" -And $_.Name -NotLike "PS*" }`