PowerShell/docs/FAQ.md

105 lines
4 KiB
Markdown
Raw Normal View History

2017-03-30 02:13:19 +02:00
# Frequently Asked Questions
## Where can I learn PowerShell's syntax?
[SS64.com](https://ss64.com/ps/syntax.html) is a good resource.
Update docs.microsoft.com links (#12653) # PR Summary * remove explicit en-us from links * remove view parameter ## PR Context follow-up #7013 ## PR Checklist - [x] [PR has a meaningful title](https://github.com/PowerShell/PowerShell/blob/master/.github/CONTRIBUTING.md#pull-request---submission) - Use the present tense and imperative mood when describing your changes - [x] [Summarized changes](https://github.com/PowerShell/PowerShell/blob/master/.github/CONTRIBUTING.md#pull-request---submission) - [x] [Make sure all `.h`, `.cpp`, `.cs`, `.ps1` and `.psm1` files have the correct copyright header](https://github.com/PowerShell/PowerShell/blob/master/.github/CONTRIBUTING.md#pull-request---submission) - [x] This PR is ready to merge and is not [Work in Progress](https://github.com/PowerShell/PowerShell/blob/master/.github/CONTRIBUTING.md#pull-request---work-in-progress). - If the PR is work in progress, please add the prefix `WIP:` or `[ WIP ]` to the beginning of the title (the `WIP` bot will keep its status check at `Pending` while the prefix is present) and remove the prefix when the PR is ready. - **[Breaking changes](https://github.com/PowerShell/PowerShell/blob/master/.github/CONTRIBUTING.md#making-breaking-changes)** - [ ] None - **OR** - [ ] [Experimental feature(s) needed](https://github.com/MicrosoftDocs/PowerShell-Docs/blob/staging/reference/6/Microsoft.PowerShell.Core/About/about_Experimental_Features.md) - [ ] Experimental feature name(s): <!-- Experimental feature name(s) here --> - **User-facing changes** - [x] Not Applicable - **OR** - [ ] [Documentation needed](https://github.com/PowerShell/PowerShell/blob/master/.github/CONTRIBUTING.md#pull-request---submission) - [ ] Issue filed: <!-- Number/link of that issue here --> - **Testing - New and feature** - [x] N/A or can only be tested interactively - **OR** - [ ] [Make sure you've added a new test if existing tests do not effectively test the code changed](https://github.com/PowerShell/PowerShell/blob/master/.github/CONTRIBUTING.md#before-submitting) - **Tooling** - [x] I have considered the user experience from a tooling perspective and don't believe tooling will be impacted. - **OR** - [ ] I have considered the user experience from a tooling perspective and enumerated concerns in the summary. This may include: - Impact on [PowerShell Editor Services](https://github.com/PowerShell/PowerShellEditorServices) which is used in the [PowerShell extension](https://github.com/PowerShell/vscode-powershell) for VSCode (which runs in a different PS Host). - Impact on Completions (both in the console and in editors) - one of PowerShell's most powerful features. - Impact on [PSScriptAnalyzer](https://github.com/PowerShell/PSScriptAnalyzer) (which provides linting & formatting in the editor extensions). - Impact on [EditorSyntax](https://github.com/PowerShell/EditorSyntax) (which provides syntax highlighting with in VSCode, GitHub, and many other editors).
2020-05-15 05:29:43 +02:00
[Microsoft Docs](https://docs.microsoft.com/powershell/scripting/overview) is another excellent resource.
2017-03-30 02:13:19 +02:00
## What are the best practices and style?
The [PoshCode][] unofficial guide is our reference.
[PoshCode]: https://github.com/PoshCode/PowerShellPracticeAndStyle
2017-03-30 02:13:19 +02:00
## What are PowerShell's scoping rules?
- Variables are created in your current scope unless explicitly indicated.
- Variables are visible in a child scope unless explicitly indicated.
- Variables created in a child scope are not visible to a parent unless
explicitly indicated.
- Variables may be placed explicitly in a scope.
2017-03-30 02:13:19 +02:00
### Things that create a scope
- [functions](https://ss64.com/ps/syntax-functions.html)
- [call operator](https://ss64.com/ps/call.html) (`& { }`)
- [script invocations](https://ss64.com/ps/syntax-run.html)
2017-03-30 02:13:19 +02:00
### Things that operate in the current scope
- [source operator](https://ss64.com/ps/source.html) (`. { }`)
- [statements](https://ss64.com/ps/statements.html) (`if .. else`, `for`, `switch`, etc.)
2017-03-30 02:13:19 +02:00
## Why didn't an error throw an exception?
2017-03-30 02:13:19 +02:00
Error handling in PowerShell is a bit weird, as not all errors result in catchable exceptions by default.
Setting `$ErrorActionPreference = 'Stop'` will likely do what you want;
that is, cause non-terminating errors instead to terminate.
Read [An Introduction To Error Handling in PowerShell][error] for more information.
2019-01-31 20:44:59 +01:00
[error]: https://gist.github.com/TravisEz13/9bb811c63b88501f3beec803040a9996
2017-03-30 02:13:19 +02:00
## Where do I get the PowerShell Core SDK package?
The SDK NuGet package `Microsoft.PowerShell.SDK` is provided for developers to write .NET Core C# code targeting PowerShell Core.
PowerShell NuGet packages for releases starting from v6.0.0-alpha.9 will be published to the [powershell-core][] myget feed.
2017-11-08 22:41:53 +01:00
To use the `Microsoft.PowerShell.SDK` NuGet package, declare `PackageReference` tags in your `.csproj` file as follows:
```xml
<ItemGroup>
<PackageReference Include="Microsoft.PowerShell.SDK" Version="6.0.0-beta.9" />
<PackageReference Include="Microsoft.PowerShell.Commands.Diagnostics" Version="6.0.0-beta.9" />
<PackageReference Include="Microsoft.WSMan.Management" Version="6.0.0-beta.9"/>
</ItemGroup>
```
[powershell-core]: https://powershell.myget.org/gallery/powershell-core
2017-03-30 02:13:19 +02:00
## Why did my build fail?
2016-05-06 19:59:15 +02:00
There are few common issues with the build.
The easiest way to resolve most issues with the build is to run `Start-PSBuild -Clean`.
### Dependency changed
2016-05-06 19:59:15 +02:00
If package dependencies were changed in any `project.json`, you need to manually
2017-03-30 02:13:19 +02:00
run `dotnet restore` to update your local dependency graphs.
`Start-PSBuild -Restore` can automatically do this.
2016-05-06 19:59:15 +02:00
### Resource changed
`Start-PSBuild` automatically calls `Start-ResGen` on the very first run.
On subsequent runs, you may need to explicitly use `Start-PSBuild -ResGen` command.
Try it, when you see compilation error about *strings.
[More details](dev-process/resx-files.md) about resource.
### TypeGen
2016-08-06 03:36:15 +02:00
Similar to `-ResGen` parameter, there is `-TypeGen` parameter that triggers regeneration of type catalog.
2017-03-30 02:13:19 +02:00
## Why did `Start-PSBuild` tell me to update `dotnet`?
We depend on the latest version of the .NET CLI, as we use the output of `dotnet
2017-03-30 02:13:19 +02:00
--info` to determine the current runtime identifier.
Without this information, our build function can't know where `dotnet` is going to place the build artifacts.
You can automatically install this using `Start-PSBootstrap`.
**However, you must first manually uninstall other versions of the CLI.**
If you have installed by using any of the following means:
- `MSI`
- `exe`
- `apt-get`
- `pkg`
You *must* manually uninstall it.
Additionally, if you've just unzipped their binary drops (or used their obtain
scripts, which do essentially the same thing), you must manually delete the
folder, as the .NET CLI team re-engineered how their binaries are setup, such
that new packages' binaries get stomped on by old packages' binaries.