powershell-2.0

How to export data to CSV in PowerShell?

梦想与她 提交于 2019-11-28 08:20:19
foreach ($computer in $computerlist) { if((Test-Connection -Cn $computer -BufferSize 16 -Count 1 -ea 0 -quiet)) { foreach ($file in $REMOVE) { Remove-Item "\\$computer\$DESTINATION\$file" -Recurse Copy-Item E:\Code\powershell\shortcuts\* "\\$computer\$DESTINATION\" } } else { Write-Host "\\$computer\$DESTINATION\" } } I want to export Write-Host "\$computer\$DESTINATION\" to the CSV files so I know which computers were offline when the script ran. I am running this from a Windows 7 machine This solution creates a psobject and adds each object to an array, it then creates the csv by piping the

How to unzip a Zip File with Powershell Version 2.0?

谁都会走 提交于 2019-11-28 07:41:15
问题 This works for me with PowerShell version 4.0 or higher. But at PowerShell version 2.0 the Add-Type isn't possible (type doesn't exist). function unzip { Add-Type -Assembly “system.io.compression.filesystem” [io.compression.zipfile]::ExtractToDirectory("SOURCEPATH\ZIPNAME", "DESTINATIONPATH") } 回答1: function Expand-ZIPFile($file, $destination) { $shell = new-object -com shell.application $zip = $shell.NameSpace($file) foreach($item in $zip.items()) { $shell.Namespace($destination).copyhere(

How to strip illegal characters before trying to save filenames?

China☆狼群 提交于 2019-11-28 07:32:54
I was able to find how to use the GetInvalidFileNameChars() method in a PowerShell script. However, it seems to also filter out whitespace (which is what I DON'T want). EDIT: Maybe I'm not asking this clearly enough. I want the below function to INCLUDE the spaces that already existing in filenames. Currently, the script filters out spaces. Function Remove-InvalidFileNameChars { param([Parameter(Mandatory=$true, Position=0, ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true)] [String]$Name ) return [RegEx]::Replace($Name, "[{0}]" -f ([RegEx]::Escape([String][System.IO.Path]:

Extract a substring using PowerShell

血红的双手。 提交于 2019-11-28 05:41:28
How can I extract a substring using PowerShell? I have this string ... "-----start-------Hello World------end-------" I have to extract ... Hello World What is the best way to do that? Matt Woodard The -match operator tests a regex, combine it with the magic variable $matches to get your result PS C:\> $x = "----start----Hello World----end----" PS C:\> $x -match "----start----(?<content>.*)----end----" True PS C:\> $matches['content'] Hello World Whenever in doubt about regex-y things, check out this site: http://www.regular-expressions.info nineowls The Substring method provides us a way to

Write-Progress for 10 Minutes PowerShell

梦想的初衷 提交于 2019-11-28 04:49:08
问题 Hi all is there a way we can show progress bar for 10 minutes with statistics of percentage completed how much time remaining for 10 Minutes? using Write-Progress. 回答1: If I understand the question correctly the goal is to show some additional information in the progress messages. This can be done for example by using the Activity parameter. The script below only shows the idea (for 1 minute, for a shorter test). It should be modified in order to reflect actually needed format of the message

Run N parallel jobs in powershell

故事扮演 提交于 2019-11-28 04:45:59
I have the following powershell script $list = invoke-sqlcmd 'exec getOneMillionRows' -Server... $list | % { GetData $_ > $_.txt ZipTheFile $_.txt $_.txt.zip ... } How to run the script block ( { GetDatta $_ > $_.txt ....} ) in parallel with limited maximum number of job, e.g. at most 8 files can be generated at one time? Start-Automating The Start-Job cmdlet allows you to run code in the background. To do what you'd ask, something like the code below should work. foreach ($server in $servers) { $running = @(Get-Job | Where-Object { $_.State -eq 'Running' }) if ($running.Count -le 8) { Start

How to retrieve a recursive directory and file list from PowerShell excluding some files and folders?

穿精又带淫゛_ 提交于 2019-11-28 04:42:32
I want to write a PowerShell script that will recursively search a directory, but exclude specified files (for example, *.log , and myFile.txt ), and also exclude specified directories, and their contents (for example, myDir and all files and folders below myDir ). I have been working with the Get-ChildItem CmdLet, and the Where-Object CmdLet, but I cannot seem to get this exact behavior. The Get-ChildItem cmdlet has an -Exclude parameter that is tempting to use but it doesn't work for filtering out entire directories from what I can tell. Try something like this: function GetFiles($path =

Unblock a file with PowerShell?

会有一股神秘感。 提交于 2019-11-28 04:41:22
I am trying to have PowerShell unblock a file in Win2K8 R2. Does anyone have a pointer as to the syntax? Joey If you are using PowerShell v3, you can use the Unblock-File cmdlet. The "blocking" part is simply an alternate data stream of the file, named "Zone.Identifier". You can display it in CMD by using input redirection (no other way to get to a stream in CMD, though): H:\Downloads> more < test.exe:Zone.Identifier [ZoneTransfer] ZoneId=3 You can find them using dir /r on Windows Vista and later: 2009-10-24 12:18 54.538.056 test.exe 24 test.exe:Zone.Identifier:$DATA Also in CMD you can

How to work with Sharepoint cmdlet without installing sharepoint?

℡╲_俬逩灬. 提交于 2019-11-28 04:40:32
问题 I have to upload csv file fields into Sharepoint 2010 list using powershell. I am working in windows XP machine. When i am trying to add-pssnapin Add-PSSnapin Microsoft.SharePoint.Powershell It throws exception as "The Windows PowerShell snap-in 'Microsoft.SharePoint.Powershell' is not installed on this machine." Is it possible to work with Sharepoint Powershell cmdlet just by installing Pssnapin or by placing the necessary dlls in some location and load it? 回答1: I think the only way is to

Enumerate file properties in PowerShell

北城以北 提交于 2019-11-28 04:13:40
问题 I have seen bits of this in other questions, but I am looking for a generic way to write a function that will take a file, and list its properties in such a way that they can be used. I am aware of the function called Get-ItemProperty but it does not list the properties that I am looking for (for example, given a .avi file, it will not tell me the length, frame width, etc). Am I using the function wrong (all I am doing is: Get-ItemProperty file ) or do I have to do this a different way? I