标签: powershell

如何在PowerShell中为命令输出添加换行符?

我使用PowerShell运行以下代码,以从注册表中获取添加/删除程序的列表:

Get-ChildItem -path hklm:\software\microsoft\windows\currentversion\uninstall `
    | ForEach-Object -Process { Write-Output $_.GetValue("DisplayName") } `
    | Out-File addrem.txt
Run Code Online (Sandbox Code Playgroud)

我希望列表按每个程序的换行符分隔.我试过了:

Get-ChildItem -path hklm:\software\microsoft\windows\currentversion\uninstall `
    | ForEach-Object -Process { Write-Output $_.GetValue("DisplayName") `n } `
    | out-file test.txt

Get-ChildItem -path hklm:\software\microsoft\windows\currentversion\uninstall `
    | ForEach-Object {$_.GetValue("DisplayName") } `
    | Write-Host -Separator `n

Get-ChildItem -path hklm:\software\microsoft\windows\currentversion\uninstall `
    | ForEach-Object -Process { $_.GetValue("DisplayName") } `
    | foreach($_) { echo $_ `n }
Run Code Online (Sandbox Code Playgroud)

但是当输出到控制台时,所有这些都导致奇怪的格式化,并且当输出到文件时,每行后面都有三个方格字符.我想Format-List,Format-Table和Format-Wide没有运气.最初,我认为这样的事情会起作用:

Get-ChildItem -path hklm:\software\microsoft\windows\currentversion\uninstall `
    | ForEach-Object -Process { "$_.GetValue("DisplayName") …
Run Code Online (Sandbox Code Playgroud)

powershell newline

65
推荐指数
5
解决办法
33万
查看次数

重命名文件夹中的多个文件,添加前缀(Windows)

我想批量重命名文件夹中的文件,将文件夹的名称添加到新名称前面.即C:\house chores\所有文件都将被重命名house chores - $old_name.

windows powershell filenames prefix batch-rename

65
推荐指数
6
解决办法
20万
查看次数

从命令行忽略"安全警告"运行脚本

我试图从我信任的共享文件夹中执行脚本:

PowerShell -file "\\server\scripts\my.ps1"
Run Code Online (Sandbox Code Playgroud)

但是我收到安全警告,并且必须按'R'才能继续

安全警告仅运行您信任的脚本.虽然来自Internet的脚本很有用,但此脚本可能会损害您的计算机.你想运行\ server\scripts\my.ps1吗?[D]不运行[R]运行一次[S]暂停[?]帮助(默认为"D"):d

我可以忽略这个警告吗?我想要的伪代码是:

PowerShell -IGNORE_SECURITY_WARNING -file "\\server\scripts\my.ps1"
Run Code Online (Sandbox Code Playgroud)

security powershell

64
推荐指数
4
解决办法
14万
查看次数

PowerShell中的排除列表Copy-Item似乎不起作用

我有以下PowerShell脚本片段:

$source = 'd:\t1\*'
$dest = 'd:\t2'
$exclude = @('*.pdb','*.config')
Copy-Item $source $dest -Recurse -Force -Exclude $exclude
Run Code Online (Sandbox Code Playgroud)

这适用于将所有文件和文件夹从t1复制到t2,但它只排除"root"/"first-level"文件夹中的排除列表,而不是子文件夹中的排除列表.

如何使其排除所有文件夹中的排除列表?

powershell

64
推荐指数
5
解决办法
7万
查看次数

PowerShell中的"退出"究竟是什么?

您可以通过键入来退出PowerShell exit.到现在为止还挺好.但究竟是什么呢?

PS Home:\> gcm exit
Get-Command : The term 'exit' is not recognized as the name of a cmdlet, function, script file, or operable program. Ch
eck the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:4
+ gcm <<<<  exit
    + CategoryInfo          : ObjectNotFound: (exit:String) [Get-Command], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException,Microsoft.PowerShell.Commands.GetCommandCommand
Run Code Online (Sandbox Code Playgroud)

所以它既不是cmdlet,也不是函数,脚本或程序.它留下的问题究竟是什么.

遗憾的是,这也意味着无法创建别名exit:

PS Home:\> New-Alias ^D exit
PS Home:\> …
Run Code Online (Sandbox Code Playgroud)

powershell

64
推荐指数
1
解决办法
11万
查看次数

使用IDE时从Powershell调用第三方可执行文件时出错

我有一个PowerShell脚本,它使用du.exe(最初来自Sysinternals的磁盘使用情况)来计算目录的大小.

如果我du c:\Backup在控制台中运行,它按预期工作,但在ISE或PowerGui中运行的相同代码行给出了预期的结果加上错误

+ du <<<<  c:\backup
+ CategoryInfo          : NotSpecified: (:String) [], RemoteException
+ FullyQualifiedErrorId : NativeCommandError
Run Code Online (Sandbox Code Playgroud)

这是为什么?如何避免此错误?我尝试使用invoke-expression &,但是没有去.

谢谢您的帮助.

ide powershell du powergui

64
推荐指数
3
解决办法
5万
查看次数

PSCustomObject到Hashtable

将a转换PSCustomObject为a 的最简单方法是Hashtable什么?它显示就像一个带有splat运算符,花括号和看似是键值对的那个.当我尝试将它投射到[Hashtable]它不起作用.我也试过.toString(),分配的变量说它是一个字符串,但没有显示任何 - 任何想法?

powershell hashtable pscustomobject

64
推荐指数
4
解决办法
5万
查看次数

PowerShell中的变量范围

PowerShell的一个令人遗憾的事情是函数和脚本块是动态范围的.

但另一件让我感到惊讶的事情是变量在内部范围内表现为写时复制.

$array=@("g")
function foo()
{
    $array += "h"
    Write-Host $array
}

& {
    $array +="s"
    Write-Host $array
}
foo

Write-Host $array
Run Code Online (Sandbox Code Playgroud)

输出是:

g s
g h
g
Run Code Online (Sandbox Code Playgroud)

这使动态范围变得不那么痛苦.但是我如何避免写时复制?

powershell scope

64
推荐指数
4
解决办法
12万
查看次数

Powershell Get-ChildItem目录中的最新文件

我们在名称中生成带日期的文件.(*下面是日期的通配符)我想获取最后一个文件,包含该文件的文件夹在其标题中也有一个日期(仅限月份).

我正在使用PowerShell,我计划每天运行它.到目前为止这是脚本:

  $LastFile = *_DailyFile
  $compareDate = (Get-Date).AddDays(-1)
  $LastFileCaptured = Get-ChildItem -Recurse | Where-Object {$LastFile.LastWriteTime        
         -ge $compareDate}
Run Code Online (Sandbox Code Playgroud)

powershell

64
推荐指数
4
解决办法
9万
查看次数

将安全字符串转换为纯文本

我正在使用power shell,我有成功将用户输入的密码转换为纯文本的代码:

$SecurePassword = Read-Host -AsSecureString  "Enter password" | convertfrom-securestring | out-file C:\Users\tmarsh\Documents\securePassword.txt
Run Code Online (Sandbox Code Playgroud)

我已经尝试了几种方法将其转换回来,但它们似乎都没有正常工作.最近,我尝试过以下内容:

$PlainPassword = Get-Content C:\Users\tmarsh\Documents\securePassword.txt

#convert the SecureString object to plain text using PtrToString and SecureStringToBSTR
$BSTR = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($PlainPassword)
$PlainPassword = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR)
[Runtime.InteropServices.Marshal]::ZeroFreeBSTR($BSTR) #this is an important step to keep things secure
Run Code Online (Sandbox Code Playgroud)

这也给我一个错误.

Cannot convert argument "s", with value: "01000000d08c9ddf0115d1118c7a00c04fc297eb0100000026a5b6067d53fd43801a9ef3f8ef9e43000000000200000000000366000
0c0000000100000008118fdea02bfb57d0dda41f9748a05f10000000004800000a000000010000000c50f5093f3b87fbf9ee57cbd17267e0a10000000833d1d712cef01497872a3457bc8
bc271400000038c731cb8c47219399e4265515e9569438d8e8ed", for "SecureStringToBSTR" to type "System.Security.SecureString": "Cannot convert the "01000000
d08c9ddf0115d1118c7a00c04fc297eb0100000026a5b6067d53fd43801a9ef3f8ef9e430000000002000000000003660000c0000000100000008118fdea02bfb57d0dda41f9748a05f10
000000004800000a000000010000000c50f5093f3b87fbf9ee57cbd17267e0a10000000833d1d712cef01497872a3457bc8bc271400000038c731cb8c47219399e4265515e9569438d8e8
ed" value of type "System.String" to type "System.Security.SecureString"."
At C:\Users\tmarsh\Documents\Scripts\Local Admin Script\PlainTextConverter1.ps1:14 char:1
+ …
Run Code Online (Sandbox Code Playgroud)

powershell securestring

64
推荐指数
4
解决办法
8万
查看次数