1

Run the following command in both command prompt and powershell.

The first one displays instantaneously in command prompt while it takes a little longer in powershell. What might be the reason that it does this?

type file

Is there a way to change or speed up the default powershell behavior?

I am happy to post a video/gif if needed.

Here is a video I'll post a gif if I can at some point https://vid.me/ZjfA

Lime
  • 13,400
  • 11
  • 56
  • 88

2 Answers2

4

type in command prompt reads text. In PowerShell, it reads lines of text and constructs .NET arrays containing a string object per line of file.

(Get-Content is slow and slow and slow and slow and slow and slow).

Community
  • 1
  • 1
TessellatingHeckler
  • 27,511
  • 4
  • 48
  • 87
  • Well how do I fix this for `type` and `dir` then? I find powershell almost unusable in my opinion because it is so slow. – Lime Oct 20 '15 at 04:12
  • @William If you really want to use `type` then call it explicitly. `Get-Content `is slower as it is doing more! It can be advantageous in many situations where type would not. Tess didnt say it exactly but `type` in PowerShell is an alias for `Get-Content` hence the difference that _appears_ to be the same command. – Matt Oct 20 '15 at 05:14
  • There are a couple of fixes mentioned in those links; you can pass a parameter to get-content to make it read more at once, or if you are reading into a variable, change to not do that just stream the output, or you can use the .Net file reader methods. If you are just using plain `type file.txt` in the shell to see what's in a file - I don't know that there is anything you can do. (But it seems odd that that use would be unusably slow - how big files and how slow? How slow for dir?). aliases are slower - test Get-ChildItem and Get-Content the full names. @Matt - oops, forgot that bit! – TessellatingHeckler Oct 20 '15 at 05:23
  • @Matt How do I call it explicitly then? @@TessellatingHeckler I don't mind trying a couple of them but which would likely be the fastest? You linked to a lot of different pages. – Lime Oct 20 '15 at 07:18
  • @TessellatingHeckler I don't believe this is exactly right(although I can't tell). I timed the `Get-Content` command and they appear to be about the same speed as command prompt so there is something internally going on when displaying. This is silly but does almost works `cmd /k "type .\Telnet2.cs"` it just runs the command prompt command but it doesn't seem to exit as it stands. – Lime Oct 20 '15 at 15:10
  • `cmd /K` is *"run this command, then stay in the command prompt"*. If you want to exit after, use `cmd /C`. What isn't exactly right? Note that if you run the command prompt from PowerShell, it's reading the text coming out of the command prompt, and doing all the same splitting it into lines and creating .Net string objects, feeding them through the pipeline and auto-formatting the output for display that Get-Content is doing. You can only really compare the command prompt speed by testing it away from PowerShell. – TessellatingHeckler Oct 20 '15 at 15:26
  • @TessellatingHeckler You are right I'm not really sure then what to do then they appear to be basically the same speed then. Well actually sometimes it appears `cmd /c "dir"` is faster I might post a video. – Lime Oct 20 '15 at 17:02
  • I don't get how you've gone from "it's almost unusable" to "they're the same speed", but... *I'm not really sure then what to do* ... if they're the same speed now, why do anything? – TessellatingHeckler Oct 20 '15 at 17:24
  • @TessellatingHeckler `ls` and `dir` appear to be the same speed when when timing them but, it **appears** running `cmd /c dir` is faster then `dir`. Something is going on that I don't really understand. – Lime Oct 25 '15 at 02:21
  • `dir` is a PowerShell alias for `Get-ChildItem`, same as `ls`. It exists for backwards compatibility and familiarity with the command prompt. `cmd /C dir` is running classic dir built into the command prompt, no .NET involved for that one. – TessellatingHeckler Oct 26 '15 at 00:13
0

First open command prompt then run powershell. Or press Win+r and then type powershell. Next type the run the following

powershell.exe -noexit -File "C:\Users\a\Desktop\test.ps1"

test.ps1

Remove-Item alias:ls
Remove-Item alias:cat
Remove-Item alias:echo
function ls {cmd /c "dir $args"}
function cat {cmd /c "type $args"}
function echo {cmd /c "echo $args"}

Now your commands should appear to be much faster.

Lime
  • 13,400
  • 11
  • 56
  • 88