Reputation: 123
I've been trying to use PowerShell to quickly search through Windows files and settings (faster than GUI), but have encountered some difficulties in improving search speed and I couldn't find solutions for them on the web.
First, I'd like to make a cmdlet which displays my own text on demand (I want to put there frequently used commands defined by me because 'get-command' is way too big for a quick search, but I don't know how do it. I made a simple script with the 'echo' cmdlet, but it's displayed only once.
Secondly, how to merge together Set-Location, Get-ChildItem and Sort-Object to quickly change directory and automatically display the list of directory content, sorted by name? It could be a custom cmdlet.
Thirdly, how to merge together Set-Location with Push-Location to easily type 'popd' to return to the previous directory?
Upvotes: 0
Views: 1394
Reputation: 32180
I think your language is a bit confused here. Actual Cmdlets are written in .Net.
However, I don't think that's what you want. The tasks you describe could be accomplished with script files and functions, which are about an order of magnitude less complicated to develop.
First I'd like to make own cmdlet which displays my own text on demand (I want to put there frequently used commends defined by me, cuz 'get-command' is way to big for quick search, but don't know how do it. I made simply script with 'echo' cmdlet, but it's shown only once.
This sounds like a script. See Get-Help about_Scripts.
Secondly how to merge together Set-Location, Get-ChildItem and Sort-Object to quickly change directory and automatically display sorted by name list of dir content? It could be custom cmdlet or so.
You can create a script with parameters to accomplish this pretty easily (well, easily when you're familiar with it).
Thirdly how to merge together Set-Location with Push-Location to easily type 'popd' to return to previous dir?
Was this an example? Pop-Location
already exists, and it already has an alias of popd
.
If you want to look at MS documentation, I would start with Get-Help about_Scripts. It also might be worthwhile to complete the Getting Started with PowerShell 3.0 Jump Start class at Microsoft Virtual Academy, which is one of the few tutorials of any kind I've heard nothing but good things about. The stuff you're talking about begins in part 9, but honestly, you sound new enough that you would probably benefit from the entire class.
There is some Microsoft doc that would qualify as a howto, but it's pretty hit-and-miss, honestly. The biggest problem with PowerShell's documentation overall is that it's a good technical reference, a mediocre cookbook, a bad user manual, and doesn't even pretend to be a tutorial. It's meant for people who already know how to script to use.
I'm not aware of any other good online tutorials, but you might want to check elsewhere like the /r/PowerShell subreddit.
Upvotes: 4
Reputation: 801
Get-Help about_advanced_functions
The term you are looking for is functions, specifically advanced functions with cmdletbinding(). This lets you build your own functions which act as commandlets, even to debug, whatif, named paramaeter sets, pipleline input, etc.
Get-Help about_Profiles
Store your commonly used functions in a file and use your profile to execute it every time.
Get-Help alias
get-alias
OR gal
lists all the currently defined aliases. Almost all commonly used commands have CLI aliases. (Using them in scripts renders your scripts unreadable, in opinion of many, including me.)
Use the aliases commandlets and the profile to set up persistent aliases.
Combine your functions into a module which can be autoloaded, and shared.
Edit, Start Snippets
or CTRL+J
Get-ChildItem
or gci
or dir
or ls
allows you to specify a path (positional parameter), and the default sort is by name.
If want to change directory, and force directory listing sorted by name, you could use cd c:\windows;gci|sort name
or sometihng like this
(pushd and popd are already aliases)
Push-Location
# do stuff
Pop-Location
or
pushd
# do stuff
popd
get-help
, get-member
, get-alias
, get-command
all very powerful tools. Keep in mind that Set-Location and Get-ChildItem and all the -Location and -Item commands can be used in more than just the lettered drives. They can be used in any PSDrive. PSDrives include Registry, AD, Env, Alias, Variable, Function, others added by modules, etc. So, give get-psdrive
a drive and see how learning the command in one place, you're really learning many others at the same time.
Upvotes: 1
Reputation: 40828
I think what you want to do is define your own functions/aliases in your profile, which is loaded every time you start the shell.
To create a blank profile (you may want to test that you don't already have one with Test-Path $profile
):
New-Item $profile -Type File -Force
Then you can edit it in notepad, ISE, or whatever and add functions and aliases.
Functions are functionally similar to cmdlets except they are written in powershell. Cmdlets are actually written in .NET dlls. Aliases (e.g. cd) simply point to cmdlets/functions.
Modifying existing cmdlets is an advanced topic but this can be done through proxy functions. However, I think you should stick to just defining your own functions and aliases for now and come back with a more specific question on proxy functions if need be.
Upvotes: 0