简体   繁体   中英

How to select the powershell command from Alias and Run that command

What i want to do is strip the output of the Alis command and get the power-shell command from it and run that command.

how i would do this in bash would be i would cut the input and get the second column then store each line in a list. then get the line that starts with the command i want and use a regular expression to get the seperateor "-> " and then put the result after that in a string and run that command. kinda like this

(if alias worked the same in bash that it does in powershell)

output of alias cd

CommandType    Name                  Version           Source
-----------    ----                  -------           ------ 
Alias          cd -> Set-Location

What i want to do is grab the Set location from the command and store that into a string to run the command latter. the fields is not tab delimited they are space delimited.

how i would do this in bash would be i would cut the input and get the second column then store each line in a list. then get the line that starts with the command i want and use a regular expression to get the seperateor "-> " and then put the result after that in a string

Fortunately for you, PowerShell cmdlets emit objects instead of text, so it's much easier to extract information like this.

To get the definition of an alias, select the definition property of the object output by get-alias :

get-alias cd |select-object -expandproperty definition

In short, anytime you start thinking "I'll parse the text output of this cmdlet" in PowerShell, you're probably doing it wrong. Instead, pipe the cmdlet to Get-Member and look at the properties and methods hanging off the output object(s). Then interrogate those. PowerShell is object-based, not text-based. If you've been living in bash for a few years/decades, it's an adjustment you have to make.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM