Powershell - Useful Commands

Don’t be sad about that. Use Visual studio code instead. In my opinion it is actually better than ISE

1 Like

Yeah no. Going to be super sad about it. I’ve been trying to get used to working in Visual Studio Code, but the down side of it is that it’s designed to look and work like Visual Studio, which has not made a lick of sense to me over several attempts to work with it. I’m just going to move over to Atom and keep all of my scripting in one place.

Not look, but have cherry picked features implemented in a text editor which would otherwise not be there; like debugging or intellisense. Its UI is actually highly similar to atom.

I have used atom, but found VSCode to be more performant and intuitive to my workflow; but everyones is different so just do what works best.

I’ve had the same experience, vscode has been for me essentially a better atom in every way. It runs and performs better, it does everything I had atom doing, it even has repos for Linux distros (unlike atom) so its kept up to date, the various plugins have also been very good.

2 Likes

Everyone at my job used to use sublime until I got them on the vscode bandwagon :wink:

1 Like

Well, I will give it another shot in the future, I’m sure. I do like to revisit things that I did not like in the past.

Okay I’m back in VSCode. This is a little different than I remember. So yeah, this is very much like Atom. I don’t have git integration yet, but I bet it’s out there. For replacing Powershell ISE, is there a way to just execute the script in the current tab? A quick Google search is failing me, though I am finding interesting ways to customize VSCode.

Edit:
Regarding git integration. I see it now. It’s in another menu area thingey.

yes, you can can run scripts with the tasks option at the top.

The fork symbol.

Huh. The Tasks menu is the first place I went, but that seems like a significantly more involved process for something other than just running a script.

When I’m running a task through npm that I have defined in a package.json I have access to those commands via the tasks menu.

Maybe you need to install plugins first to get the tooling, OOTB I’m not sure what all all it comes with that isn’t geared towards web dev.

Since I’ve started messing with Powershell, I noticed commands Push-Location and Pop-LocationThey’re aliased to pushd and popd by default but until now I haven’t exactly had an idea on how to make them useful. Digging through the Help page for Get-Location, I noticed that it can call stacks created from push/popd.

It’s fairly straightforward, Get-Location -Stack and that will show… A list of the directories in the stack. shocker But this isn’t exactly the most useful, especially since the output isn’t useful for scripts. The way around this is to call the Path property… (Get-Location -Stack).Path And use ForEach-Object to make use of each item in the stack.

What's a stack, and how's it useful?

If you’re not familiar with stacks, at least in context of working with directories, check out the L1Linux video on using BASH. It’s the first topic they cover.

How about outside of scripting purposes? Calling an entire stack in general use isn’t always useful. When I’m using BASH I tend to use dirs -v in conjunction with the ~ operator… Unfortunately there isn’t a 1-1 for this… But there is still a way to get at an individual path it’s just somewhat cumbersome to use.

(Get-Location -Stack).Path[n] and replace n with whichever number item in the stack you’re looking to use… Starting from 0 as the top item in the output from Get-Location -Stack. Seems easy enough, even though it puts the directories in… alphabetical order? Yes, it’s annoying it doesn’t keep them in the order that the directories were pushed.


This whole time I’ve only been talking about working with the default stack. While I haven’t had an actual need for creating multiple directory stacks yet. I do see that it could be potentially useful at some point or another.

push/popd have an arguement -StackName which will either create or modify a stack other than the default. Check out the Help page for some more detail on how to use it. I like putting the stack name into a $variable so I don’t forget/fatfinger the stack name

As for calling the different stacks, Get-Location -StackName name is all it takes. Everything above still applies, and works just fine.


I mentioned dirs -v earlier, which gives a numbered listing of the items in a stack. Which is very useful for larger stacks… I’ve been working on a function to try to help deal with this, and it’s sort-of to a point where it’s useful.

function Get-DirStack {
param($stackName = 'default')

$stackSize = (Get-Location -StackName $stackName).Count
$currentItem = 0
$ds = (Get-Location -StackName $stackName).Path
while ($currentItem -lt $stackSize) {
    $properties = [ordered]@{
        Id = $currentItem;
        Stack = $stackName;
        Dir = $ds[$currentItem];
        
    }
    New-Object -TypeName psobject -Property $properties
    
    $currentItem++
}

Remove-Variable stackSize, currentItem
}

Get-DirStack will display a numerical list of a given stack, and will show the default stack by default. It’s not much, but it gets the job done.

I’ve been trying to think of a reasonable way to get similar functionality to the ~ in BASH, but as of now I’m stumped.

3 Likes