powershell-2.0

Sort Hashtable and assign it to a new variable in Powershell

百般思念 提交于 2020-01-23 16:59:13
问题 I would like to know how to assign a sorted hasttable using the following command to a new variable. I am using the following command $ht = @{ 1 = "Data1"; 3 = "Data3"; 2 = "Data2"; } $htSorted = $ht.GetEnumerator() | Sort-Object Name but after running it i can't traverse on the resultant hashtable foreach($key in $htSorted.Keys) { $item[$key] } Any idea ? 回答1: In case you need to have a sorted hashtable, you can use SortedDictionary . Example: $s = New-Object 'System.Collections.Generic

Why won't PowerShell ISE let me set breakpoints when running as Administrator?

倾然丶 夕夏残阳落幕 提交于 2020-01-23 06:45:08
问题 Having failed to get PowerGUI to work properly I've fallen back on PowerShell ISE. However if I run this as Administrator it won't let me set any breakpoints. If I launch as normal (my login is a member of Administrators) all is well but because I need to script some tasks that require full administrator rights this is no good. I have saved the script as a proper file (i.e. it's not "untitled1.ps1") but no joy. I'm running Windows 7 x64 Ultimate. Strangely this doesn't seem to be a problem on

Automate Virtual PC 2007 with PowerShell?

﹥>﹥吖頭↗ 提交于 2020-01-22 13:46:04
问题 This is basically a duplicate of this question, but the accepted answer was "no" and I would like to keep this question open until getting an actual answer instead of accepting "NO" and giving up. Stephen Rose told me via Twitter DM to use PowerShell to start and stop Virtual PC VM's and do things like run installations, automatic updates and virus scans, but he hasn't yet responded to my request for any links to resources that describe how to do so. I've started learning PowerShell, but I

Setting playback device by executing a batch file / powershell script

允我心安 提交于 2020-01-22 04:42:17
问题 I've got my computer(Windows 7) hooked up to the TV, and i very often change output device for sound between Digital Audio (S/PDIF)(High definition audio device) and my headset (2- Corsair CA-HS1 USB Headset) I wanna be able to execute a batch/script file who changes this for me so i don't have to "right click volume > playback devices > "Mark output device" and click "set default". I know it's a luxury problem, but hey, maybe I can learn something from someone? All help appreciated! 回答1: I

How to get the next business day in powershell

让人想犯罪 __ 提交于 2020-01-22 00:43:09
问题 I have the following test code. Basically I am checking when a new file is created in folder. I need to know that if the file was created after 4pm display the next business day. Currently my code displays the next day, but I need to display the next business day. Any help would be appreciated. $formatteddate = "{0:h:mm:ss tt}" -f (get-date) if ($formatteddate -gt "4:00:00 PM"){ $(Get-Date).AddDays(1).ToString('MMM d yyyy') } 回答1: Adding to what jisaak said: "business day" is organization

Directly Referencing Properties in Powershell 2.0

↘锁芯ラ 提交于 2020-01-21 20:41:50
问题 I encountered a bug in a script I wrote this morning where I wasn't getting an output from my Select-String expression. After a bit of playing I realized that this expression would not return the value of my match in v2.0 but would in v4.0 where I originally wrote it. ($log | Select-String "\[CIsoCreator\] Creating iso file" -AllMatches | Select-Object -ExpandProperty line -Last 1 | Select-String "([A-Z]\:)(.*\\)*.*\.iso").matches.value After trying a few things I ended up with this which

Directly Referencing Properties in Powershell 2.0

坚强是说给别人听的谎言 提交于 2020-01-21 20:41:12
问题 I encountered a bug in a script I wrote this morning where I wasn't getting an output from my Select-String expression. After a bit of playing I realized that this expression would not return the value of my match in v2.0 but would in v4.0 where I originally wrote it. ($log | Select-String "\[CIsoCreator\] Creating iso file" -AllMatches | Select-Object -ExpandProperty line -Last 1 | Select-String "([A-Z]\:)(.*\\)*.*\.iso").matches.value After trying a few things I ended up with this which

How to properly escape quotes in powershell v2?

时间秒杀一切 提交于 2020-01-21 05:52:10
问题 How do you properly escape quotes in powershell v2 (called from within a batch file)? I have tried: powershell -Command "(gc file1.txt) -join "`n" | Out-File file2.txt" and powershell -Command "(gc file1.txt) -join ""`n"" | Out-File file2.txt" and powershell -Command "(gc file1.txt) -join '"`n`" | Out-File file2.txt" but they all fail. Editor's note : The purpose of the command is to transform Windows CRLF line breaks to Unix LF-only ones, so as to create a file that will be processed on

Powershell select-string fails due to escape sequence

泄露秘密 提交于 2020-01-16 07:24:32
问题 I am having a file which has following contents change sets: promotion level: INITIAL depends on: GEESF_R2.1.0.9.5179@\My_PVOB (MES@\My_PVOB) My_2.1.0.13.4875@\My_PVOB (Notification@\My_PVOB) MyComponents_8_8_2011.6859@\My_PVOB (SQLReporting@\My_PVOB) My_2.1.0.13.7098@\My_PVOB (Support@\My_PVOB) I wanted to read the contents which has pattern @\My_PVOB) So i write select-string option like this. Select-string -pattern "@\My_PVOB)" -path "C:\Baselines.txt" But i am getting following issue

How to remove words from all text files in a folder?

不想你离开。 提交于 2020-01-15 04:59:10
问题 I have a code like this: $txt = get-content c:\work\test\01.i $txt[0] = $txt[0] -replace '-' $txt[$txt.length - 1 ] = $txt[$txt.length - 1 ] -replace '-' $txt | set-content c:\work\test\01.i It just removes a - from first line and last line in a text file, but I need to do this for all text files in the directory tree which contains over 5k text files. how should I modify this code? All name of text files are random. I need to do this in powershell. Its a directory tree, under c:\work\test