2

I mean to colorize the output of ls. I checked Powershell: Properly coloring Get-Childitem output once and for all.

The two options appear to be:

  1. Use New-CommandWrapper, as advocated in the OP and the answer by Jon Z.
  2. Use module PSColor.

I obtained the code for New-CommandWrapper from the OP (it is the same as provided by O'Reilly), placed it in file New-CommandWrapper.ps1, and dot source it in my profile.ps1. Now when I open a new session I get

cmdlet New-CommandWrapper.ps1 at command pipeline position 1
Supply values for the following parameters:
Name:

Is this ok? If so, what should I enter? Or how do I fix this? (I am certain the issue is quite simple).

Note: I couldn't make PSColor work, and this is possibly worth a another question. As mentioned in a comment, after importing the module, (almost?) any cmdlet outputs

Value cannot be null.
Parameter name: command
En línea: 39 Carácter: 9
+         $steppablePipeline.Begin($PSCmdlet)
+         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~`

EDIT: With the modification suggested by Thomas, the problem changed. Now I get the same Value cannot be null. error. I would like to conclude that his suggestion is right, and I solved one of the many chained problems I had... but I cannot be sure.

1 Answers1

2

As mentioned in the comments, New-CommandWrapper is packaged as a script, rather than as a function, so you'll need to edit the script file slightly if you want to dot-source it:

  1. Insert function New-CommandWrapper { at the very top of New-CommandWrapper.ps1
  2. Add an } on the very last line

Now you can dot-source it (from your profile if need be) and use the example given in the linked answer:

PS C:\> . .\path\to\New-CommandWrapper.ps1
PS C:\> New-CommandWrapper Out-Default `
>>>    -Process {
>>>        if(($_ -is [System.IO.DirectoryInfo]) -or ($_ -is [System.IO.FileInfo]))
>>>        {if(-not ($notfirst)) {
>>>           Write-Host "    Directory: $(pwd)`n"           
>>>           Write-Host "Mode                LastWriteTime     Length Name"
>>>           Write-Host "----                -------------     ------ ----"
>>>           $notfirst=$true
>>>           }
>>>           if ($_ -is [System.IO.DirectoryInfo]) {
>>>           Write-host ("{0,-7} {1,25} {2,10} {3}" -f $_.mode, ([String]::Format("{0,10}  {1,8}", $_.LastWriteTime.ToString("d"), $_.LastWriteTime.ToString("t"))), $_.length, $_.name) -foregroundcolor "yellow" }
>>>           else {
>>>           Write-host ("{0,-7} {1,25} {2,10} {3}" -f $_.mode, ([String]::Format("{0,10}  {1,8}", $_.LastWriteTime.ToString("d"), $_.LastWriteTime.ToString("t"))), $_.length, $_.name) -foregroundcolor "green" }
>>>           $_ = $null
>>>        }
>>>} 
Mathias R. Jessen
  • 157,619
  • 12
  • 148
  • 206
  • This works. After some debugging, I found the culprit for all the pain: I had added at the top of my `profile.ps1` the code suggested at the top of [this answer](https://stackoverflow.com/a/62838107/2707864). It worked mostly ok. But in some cases (as in this case, with both methods for colorizing `dir` output), by "interfering" with other commands, it was producing the `Value cannot be null.` error. Moreover, if I added that code below the scripts for colorizing `dir` output, it did not produce an error upon `dir`, but output was simply white. – sancho.s ReinstateMonicaCellio Jul 26 '20 at 08:23