19

请提供您认为有用的一行 PowerShell 脚本,每个答案一个脚本。

这里有一个类似的问题,但是这个问题只提供了带有脚本的页面的链接,让我们在这里一一回答,并提供最常用或最有用的脚本的贡献列表。

  1. 列出最新版本的文件

    ls -r -fi *.lis | sort @{expression={$_.Name}}, @{expression={$_.LastWriteTime};Descending=$true} | select Directory, Name, lastwritetime | Group-Object Name | %{$_.Group | Select -first 1}

  2. gps programThatIsAnnoyingMe | kill

  3. 使用其注册程序打开一个文件(start例如start foo.xls

    ii foo.xls

  4. 检索并显示系统特殊文件夹的路径

    [enum]::getvalues([system.environment+specialfolder]) | foreach {"$_ maps to " + [system.Environment]::GetFolderPath($_)}

  5. 将环境值复制到剪贴板(所以现在你知道如何使用剪贴板了!)

    $env:appdata | % { [windows.forms.clipboard]::SetText($input) }
    或者
    ls | clip

使用管理单元

  1. TFS 中两个变更集编号之间的文件

    Get-TfsItemHistory <location> -Recurse -Version <label1>~<label2> | % { $(Get-TfsChangeset $_.ChangeSetID).Changes } | % { $_.Item.ServerItem } | Sort-Object -Unique

  2. 获取所有 Hub 服务器上的错误队列消息以交换 200

    Get-ExchangeServer | ?{$_.IsHubTransportServer -eq $true} | Get-Queue | ?{$_.LastError -ne $null} | Sort-Object -Descending -Property MessageCount | ft -Property NextHopDomain,@{l="Count";e={$_.MessageCount}},@{l="Last Try";e={$_.LastRetryTime.tosting("M/dd hh:mm")}},@{l="Retry";e={$_.NextRetryTime.tostring("M/dd hh:mm")}},Status,LastError -AutoSize

4

19 回答 19

21

在下午 6:00 左右......

exit
于 2009-06-03T15:36:41.897 回答
13

列出我今天更新的所有文件:

dir | ?{$_.LastWriteTime -ge [DateTime]::Today}

经常使用它,以至于我实际上在我的个人资料中创建了一个小功能:

function Where-UpdatedSince{
Param([DateTime]$date = [DateTime]::Today,
      [switch]$before=$False)
Process
{ 
    if (($_.LastWriteTime -ge $date) -xor $before)
    {
        Write-Output $_
    }
} 
};  set-item -path alias:wus -value Where-UpdatedSince

所以我可以说:

dir | wus
dir | wus "1/1/2009"

要查看今天之前更新的内容:

dir | wus -before
于 2009-03-05T20:30:30.227 回答
13

Well, here is one I use often along with some explanation.

ii .

The ii is an alias for Invoke-Item. This commandlet essentially invokes whatever command is registered in windows for the following item. So this:

ii foo.xls

Would open foo.xls in Excel (assuming you have Excel installed and .xls files are associated to Excel).

In ii . the . refers to the current working directory, so the command would cause windows explorer to open at the current directory.

于 2009-03-05T16:33:50.793 回答
6

我最喜欢的 powershell 一班轮

gps programThatIsAnnoyingMe | kill
于 2009-03-05T15:37:16.373 回答
4
($x=new-object xml).Load("http://rss.slashdot.org/Slashdot/slashdot");$x.RDF.item|?{$_.creator-ne"kdawson"}|fl descr*

我最喜欢的:这是一个 slashdot 阅读器,没有先生的可怕提交。科道森 它被设计为少于 120 个字符,因此可以用作 / 上的签名。

于 2009-03-18T00:29:51.973 回答
3

检索并显示系统特殊文件夹的路径

[enum]::getvalues([system.environment+specialfolder]) | foreach {"$_ maps to " + [system.Environment]::GetFolderPath($_)}
于 2009-03-10T21:02:35.817 回答
3

我不喜欢计算代码行数的复杂应用程序,尤其是因为我认为它首先是一个虚假的指标。我最终改用 PS 单线:

PS C:\Path> (dir -include *.cs,*.xaml -recurse | select-string .).Count

我只是在行数中包含我想要包含的文件的扩展名,然后从项目的根目录中获取它。

于 2009-12-18T13:13:33.543 回答
3

我发现显示环境变量的值很有用

dir env:

您也可以将 env 值复制到剪贴板

$env:appdata | % { [windows.forms.clipboard]::SetText($input) }

(您需要在调用之前加载 windows.forms:Add-Type –a system.windows.forms;并使用 -STA 开关运行 PowerShell)

于 2009-06-01T12:15:37.463 回答
3

我犹豫要一一列举我的 PowerShell 单行代码列表,因为列表编号目前只有大约 400 个条目!:-) 但这里有一些我最喜欢的,以激起你的兴趣:

  • 列出所有类型的加速器(需要PSCX):[accelerators]::get
  • 将 XML 的字符串表示形式转换为实际的 XML:[xml]"<root><a>...</a></root>"
  • 转储一个对象(增加深度以获得更多细节):$PWD | ConvertTo-Json -Depth 2
  • 通过子字符串从历史记录中调用命令(查找之前的 'cd' cmd):#cd
  • 访问 C# 枚举值:[System.Text.RegularExpressions.RegexOptions]::Singleline
  • 生成条形图(需要 Jeff Hicks 的cmdlet):ls . | select name,length | Out-ConsoleGraph -prop length -grid

整个系列在 Simple-Talk.com 上发布的 4 部分系列中公开提供——我希望这些对 SO 读者有用!

我想将这个系列称为“在 PowerShell 的一行中做任何事情”,但我的编辑想要更简洁的东西,所以我们选择了PowerShell One-Liners。尽管为了充分披露,只有 98% 左右是真正符合该术语真正精神的单行者;我认为四舍五入已经足够接近了...... :-)

于 2014-04-29T03:39:18.857 回答
3

禁止 Visual Studio 2012 ALL CAPS 菜单 - 安装 VS2012 后我做的第一件事。

Set-ItemProperty -Path HKCU:\Software\Microsoft\VisualStudio\11.0\General -Name SuppressUppercaseConversion -Type DWord -Value 1

感谢发现注册表值的Richard Banks 。

于 2012-06-01T13:35:58.077 回答
2

这显示了哪些进程正在使用哪些版本的 MS CRT DLL:

gps | select ProcessName -exp Modules -ea 0 | 
  where {$_.modulename -match 'msvc'} | sort ModuleName | 
  Format-Table ProcessName -GroupBy ModuleName
于 2009-03-22T06:57:27.710 回答
2

由于我安装了 TFS PowerTools snap,这可能是作弊,但这对于找出哪些文件在两个变更集、版本或标签之间发生了更改非常有用。

Get-TfsItemHistory <location> -Recurse -Version <label1>~<label2> | 
% { $(Get-TfsChangeset $_.ChangeSetID).Changes } |
% { $_.Item.ServerItem } | Sort-Object -Unique
于 2009-06-01T11:15:24.153 回答
1

按 alpha 顺序列出所有 Windows 提供程序:

get-winevent -listprovider microsoft-windows* | % {$_.Name} | sort

实际上,您可以将它与通配符一起用于任何特定的提供者组:

get-winevent -listprovider microsoft-windows-Securit* | % {$_.Name} | sort
于 2011-07-04T03:03:34.223 回答
1

在文件中生成一些伪随机字节。

[Byte[]]$out=@(); 0..9 | %{$out += Get-Random -Minimum 0 -Maximum 255}; [System.IO.File]::WriteAllBytes("random",$out)

Get-Random 算法从系统时钟中获取种子,因此不要将其用于严重的加密需求。

于 2013-09-27T18:27:13.020 回答
1

将某些内容的输出通过管道传输到剪贴板

ls | clip
于 2012-10-24T23:47:03.640 回答
1
cls

在我尝试的每个命令之后摆脱所有难以处理的、冗长的红色标记,让我以漂亮的豪华清晰屏幕继续。

于 2016-01-22T05:58:46.787 回答
0

功能显示的系统正常运行时间我将其用于我的会计电子表格

    function get-uptime
{
$PCounter = "System.Diagnostics.PerformanceCounter"
$counter = new-object $PCounter System,"System Up Time"
$value = $counter.NextValue()
$uptime = [System.TimeSpan]::FromSeconds($counter.NextValue())
"Uptime: $uptime"
"System Boot: " + ((get-date) - $uptime)
}
于 2009-12-18T13:07:09.747 回答
0

Gets queue messages with errors over all Hub servers in exchange 2007 (with some formatting)

Get-ExchangeServer | ?{$_.IsHubTransportServer -eq $true} | Get-Queue | ?{$_.LastError -ne $null} | Sort-Object -Descending -Property MessageCount | ft -Property NextHopDomain,@{l="Count";e={$_.MessageCount}},@{l="Last Try";e={$_.LastRetryTime.tosting("M/dd hh:mm")}},@{l="Retry";e={$_.NextRetryTime.tostring("M/dd hh:mm")}},Status,LastError -AutoSize        
于 2009-06-01T13:48:32.767 回答
0

复制一些到桌面:

Copy-Item $home\*.txt ([Environment]::GetFolderPath("Desktop"))
于 2010-02-03T06:12:03.653 回答