does powershell have a hard breakpoint keyword - powershell

Been searching around and not able to find an answer to whether or not powershell has a hard breakpoint solution. Something like javascript debugger; and python breakpoint() and vb Stop.
Instead i have to author dummy lines inside if statements that i can assign soft breakpoints to.
if ($item.Value -eq "some large iteration case i need to debug through") {
$i = "using to enable soft breakpoint on specific case for debugging purposes as powershell doesn't appear to have have hard breakpoint expression"
}

You can use Set-PSBreakpoint and enable/disable-psbreakpoint
https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/set-psbreakpoint?view=powershell-7
https://ss64.com/ps/enable-psbreakpoint.html

Download the PowerShell ISE, or use Visual Studio with the PowerShell extension.

Related

Powershell equivalent of MATLAB keyboard() command for debugging?

MATLAB has a feature to allow users to introduce a command in the script/function called keyboard and have the code stop there with a console/command prompt that gives direct access to the interpreter and the variable space. MATLAB keyboard() command docs
Is there something similar in Powershell? I don't want to deal with Powershell ISE as I don't like it's syntax highlighting (doesn't highlight all occurrences of selected phrase like Notepad++ does) and the macros is a lot weaker than what the plain Powershell console itself (like lacking F8 for recalling).
Stepping with a debugger is not quite as useful when you need to explore complex object structures and try a few things out with the current variable states, instead of just reading what the variables and values are in a docked window. It's much easier to work with the state of mind of the interpreted language at any point by interacting with it instead of ploughing through stack and trace information.
In both Powershell ISE and VSCode, you can read or update the same variables in your session from the terminal window at the bottom, even during debugging.

Is there any way to use some "shortcut" for longer command lines?

I am using the same, longer command line with some arguments every day and it would be nice, if there were some possibility to make some shortcut for it. Is there any?
Learn aliases. Of course they depends of shell you're using. Doskey for cmd, alias for powershell or tcc, and so on.
Also. ConEmu has GuiMacro feature. You may set up user hotkeys with print function.

How to type a tab (indent) in comments in PowerShell ISE?

This is weird.
I know tab is for command completion in the PowerShell ISE and fine so. But, it also messes up the editing pane.
Do this:
File > New (Untitled1.ps1 opens)
Press tab (all fine, you get an indent)
type enter, # (comment) and press tab after it
expected: one would get indentation after the hash
actual: one gets the hash replaced by $PSVersionTable or whatever the command prompt has in its history! (tab and Shift-tab circle through those)
Does this mean no-one uses tabs within comments in PowerShell scripts, or that no-one uses comments in PowerShell scripts?
Can I turn off this behavior anywhere?
Also, the behavior seems to be inconsistent. If I e.g. type ##, sometimes tab does not do the completion (it does not enter a tab either).
Can others reproduce this?
System:
Windows 8.1 Pro
PowerShell ISE
To answer the main question, you can enter Alt+09 (using numeric keypad) to enter <Tab>.
For the behavior described, I see this as expected behavior. You can get history completion by typing # and part of a previous command then pressing Tab repeatedly will cycle backwards through matching history. Typing # alone will match all history starting with the last command.
Does this mean no-one uses tabs within comments in PowerShell scripts?
Anecdotal, but I've never used tabs in a single line comment, but I do often use tabs within multiline comments which are bracketed by <# and #>. E.g.
<#
Functions
Get-Foo
Get-Bar
Variables
$Foo
$Bar
#>
Function Get-Foo { ...
With multiline comments, the auto-completion will not be an issue.
, or that no-one uses comments in PowerShell scripts?
I don't know why this would be implied by the behavior; I always use a single space to start a line comment.
I find this helpful when developing a script as I often try expressions in the command pane if I'm unsure of the behavior, then add the expression to the script if it works.
So, my workflow would be:
Ctrl-D to go to the Command Pane
Type a command
If the command did what I wanted, Ctrl-I to go to the Script Pane
Type #<Tab>, and the line is added to the script.

How to make the input not automatically execute when I paste in PowerShell

I have a feeling that this is a very simple fix, but whenever I try to paste into PowerShell using right-click, it automatically executes whatever it is I am trying to paste. The function is finding a computer name based on an input using AD and then copies the computer name to use in other functions. Here is the code for only the part that does the copying:
$strComputer = $output.Name
$strComputer | clip | Out-Null
So when I right-click to paste, it executes whatever $strComputer equals and obviously throws an error. Thanks!
The problem with pasting something into PowerShell (and clip.exe is very good example) is the fact, that any newline (and clip.exe used like that will add newline at the end) will trigger same reaction that you pressing enter would.
There are at least two ways around it:
paste without newlines (e.g. use [System.Windows.Clipboard]::SetText($output.Name))
if you are on v3 - use PSReadLine module and CTRL+V
For the first one - be sure to run Add-Type -AssemblyName PresentationCore first - this assembly is loaded by default only in PowerShell ISE. If you are on v3+ though I would really recommend to give PSReadLine a try.
I've found an easy solution, that doesn't require any Powershell modifications.
Steps:
Type if ($true) { into the terminal.
Paste in the code-block. Because you started an if-block, but didn't provide an end-bracket for it, Powershell interprets the code-block as incomplete, and so awaits the end-bracket before running anything.
Now you can use the arrow keys to edit the code block however you want.
When you're done with your edits, add the missing } to the very end, then press enter to execute it.
Alternatively you can use the ISE, where you can paste into the Script Pane (the top half), and edit until it appears as desired, and run when you want to.

Changing the hot key for run selection in Powershell ISE

I'm not sure what is the best approach for asking these question, so I'm putting them all together here. After some googling these questions didn't have an obvious answer.
1) In Powershell ISE is there a way to change the "run selection" quick-key/hot-key so that it's something other than the default (which is F8)?
2) For that matter, is there a way to add quick-key bindings without adding a menu?
3) Can one add key cords to the ISE like in Visual Studio (Ctrl-d, Ctrl-h) for example.
I don't think you can change, but you can add another key to do the same:
function invoke-selection
{
iex $psISe.CurrentFile.Editor.SelectedText
}
$psISE.CurrentPowerShellTab.AddOnsMenu.Submenus.Add("Run selection",{invoke-selection},'f7')
Add the above to your Powershell ISE profile - $profile from ISE
Simple answer in no.
Unfortunately the ISE is a very basic editor and doesn't provide much in the way of customisation.
This might change when the ISE for PSv3.0 is released.
In the mean time this post has a list of PowerShell ISEs that you might want to try:
https://stackoverflow.com/questions/171514/best-ide-for-powershell
regards
Arcass