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

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 large number of basic things and I end up using both side by side if I hit a sticky spot.

Today this was removing a directory - rd or rmdir - both of which are broken in powershell in favour of one it's undocumented (from the commandline) cmdlets Remove-Item. I seem to run into it all the time though - sc (for mucking around with services); where for finding what program is being called when you type a command etc etc.

Hilariously I actually got the problem with sc and then googled to find out the command where only to discover that didnt work in powershell either! That was a confusing day

In some cases once you know this is what's going on you can type the full exe name (for instance 'where.exe' will work whereas 'where' on its own wont).

This isn't the case with rmdir however. Although interestingly typing 'where rmdir' in cmd doesnt work.

So... my question is this - Is there a way of turning off (preferably all) cmdlets in powershell and just getting the normal stuff instead?


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:Python27python.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

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.

链接地址: http://www.djcxy.com/p/30334.html

上一篇: 从Windows批处理文件设置系统环境变量?

下一篇: 是否有可能/如何阻止使用某些cmdlet的PowerShell?