Is it possible to/how do you stop powershell using certain cmdlets?

前端 未结 2 2031
栀梦
栀梦 2021-01-15 16:09

Powershell is clearly a lot better than cmd but it hides basic functionality. I normally use it to figure out how to use commands that I want in scripts but it breaks a lar

相关标签:
2条回答
  • 2021-01-15 16:27

    There is no need to turn off cmdlets in powershell as that would destroy the very reason for having it.

    Most of the "normal" stuff is there anyway, which you can find by using the get-alias command.

    C:\> get-alias
    
    CommandType     Name
    -----------     ----
    Alias           % -> ForEach-Object
    Alias           ? -> Where-Object
    Alias           ?? -> Invoke-NullCoalescing
    Alias           ac -> Add-Content
    Alias           asnp -> Add-PSSnapin
    Alias           cat -> Get-Content
    Alias           cd -> Set-Location
    Alias           chdir -> Set-Location
    .....
    ..... AND A WHOLE LOT MORE!
    

    If you are missing a command that you really, really want to have, then you can easily add a new alias:

    Set-Alias python2 "C:\Python27\python.exe"
    

    In order to avoid having to do this every single time, you can simply add this into your startup profile. Just type in $PROFILE into the command prompt and it will show you the file path. If it doesn't exist, simply create it, and any powershell commands you add to the top will be automatically invoked when you start a new session.

    And last thing. All of the commands are documented, and you can get to them easily using just two.

    Just type this into your command prompt and you will be on your way to Powershell enlightenment!

    get-help get-command
    
    get-command -noun Item
    get-command -verb get
    
    0 讨论(0)
  • 2021-01-15 16:38

    I just realised the answer to my question was buried in the comments to the other answer:

    To remove a cmdlet in powershell you run

    Remove-Item alias:something.

    I can confirm you can do this by using the profile mentioned in Josh's post, however there are a couple more steps:

    By default you cant run scripts in powershell. You have to change this using set-ExecutionPolicy.

    I changed this by using an admin powershell and typing

    set-executionpolicy bypass
    

    This will let you run any script you like

    Then in my profile script I have lines like:

    Remove-Item -force alias:sc
    

    You wont see errors from this script when it runs and it wont do anything unless you have force.

    0 讨论(0)
提交回复
热议问题