如何更改Powershell的默认输出格式以使用Format-Table -autosize? [英] How to change default output Formatting of Powershell to use Format-Table -autosize?

查看:287
本文介绍了如何更改Powershell的默认输出格式以使用Format-Table -autosize?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何强制Powershell使用

How can I enforce powershell to use

Format-Table -auto

作为将返回的对象数组写入控制台时的默认格式? 谢谢

as a default formatting when writing a returned array of objects to the console? Thanks

推荐答案

如果每次都可以调用cmdlet,并且至少具有PowerShell v3.0,则可以设置$PSDefaultParameterValues,您可以阅读有关的更多信息在 about_Parameters_Default_Values .

If you are OK calling the cmdlet every time and if you have at least PowerShell v3.0 then you can set a $PSDefaultParameterValues which you can read more about at about_Parameters_Default_Values.

满足您需要的语法为:

$PSDefaultParameterValues=@{"<CmdletName>:<ParameterName>"="<DefaultValue>"}

因此我们将开关设置为$true来添加它.

So we add in the switch by setting it to $true.

$PSDefaultParameterValues = @{"Format-Table:Autosize"=$true}

要删除此内容,您必须执行与哈希表元素相同的操作

To remove this you would have to do it much the same as you would a hashtable element

$PSDefaultParameterValues.Remove("Format-Table:Autosize")


从前面的文章中可以找到一些有关如何处理这些问题的信息.


From the aforementioned article here is some pertinent information as to how to deal with these.

$ PSDefaultParameterValues是一个首选项变量,因此它仅存在于会话中 在其中设置.没有默认值.

$PSDefaultParameterValues is a preference variable, so it exists only in the session in which it is set. It has no default value.

要设置$ PSDefaultParameterValues,请输入变量名称和一个或多个键值对 在命令行上.

To set $PSDefaultParameterValues, type the variable name and one or more key-value pairs at the command line.

如果键入另一个$ PSDefaultParameterValues命令,则其值将替换原来的命令 价值.原始文件未保留.

If you type another $PSDefaultParameterValues command, its value replaces the original value. The original is not retained.

要为以后的会话保存$ PSDefaultParameterValues,请添加$ PSDefaultParameterValues 命令到Windows PowerShell配置文件.有关更多信息,请参见about_Profiles.

To save $PSDefaultParameterValues for future sessions, add a $PSDefaultParameterValues command to your Windows PowerShell profile. For more information, see about_Profiles.


除此之外,我不确定,因为很难从动态的角度进行更改.您可能想要确保发送到流的数据以与format-table -auto相同的方式出现在屏幕上,但是您必须确保它不会影响数据,以便无法捕获它或将其发送到管道中.


Outside of that I am not sure as it would be difficult to change in a dynamic sense. You would want to be sure that data sent to the stream appears on screen in the same way that format-table -auto does but you would have to make sure that it does not affect data so that you could not capture it or send it down the pipe.

您正在查看创建自定义输出格式文件,

You are looking at creating custom output format files, like Frode F. talks about, then you would need to consider looking at about_Format.ps1xml but you would need to configure this for every object that you would want to display this way.

FileSystem.format.ps1xml将控制Get-ChildItem的输出. Format-Table更具动态性,我认为您不能说只在该文件中使用Format-Table.

FileSystem.format.ps1xml, for example, would govern the output from Get-ChildItem. Format-Table is more dynamic and I don't think you can say just use Format-Table in that file.

这篇关于如何更改Powershell的默认输出格式以使用Format-Table -autosize?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆