Powershell – Redirect InvalidArgument Error Output to File

  Kiến thức lập trình

The (invalid) command New-NetIPAddress -InterfaceAlias Ethernet0 -IPAddress 172.1.2.3 -PrefixLengh 27 produces the following error:

New-NetIPAddress : A parameter cannot be found that matches parameter name 'PrefixLengh'.
At line:1 char:65
+ ... ddress -InterfaceAlias Ethernet0 -IPAddress 172.1.2.3 -PrefixLengh 27
+                                                           ~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [New-NetIPAddress], ParameterBindingException
    + FullyQualifiedErrorId : NamedParameterNotFound,New-NetIPAddress

I want to redirect this error message to a file.

Usually, this should work somewhat like this: New-NetIPAddress -InterfaceAlias Ethernet0 -IPAddress 172.1.2.3 -PrefixLengh 27 2>&1 > output

I have tried lots of combinations, for example *> out, 2>&1 3>&1 4>&1 5>&1 6>&1 > out, but none of them redirect the output. It always gets printed to the screen and the file remains empty. PSVersion: 5.1.22621.2506 (latest powershell on windows 11).

How can I properly redirect this error output to a file?

LEAVE A COMMENT