janikvonrotz
6/10/2013 - 3:04 PM

PowerShell: Snippets #PowerShell #SnippetContainer

PowerShell: Snippets #PowerShell #SnippetContainer

Write-Host "Finished" -ForegroundColor Green
Read-Host "Press Enter to exit"
$a = 5
switch ($a) 
    { 
        1 {"The color is red."} 
        2 {"The color is blue."} 
        3 {"The color is green."} 
        4 {"The color is yellow."} 
        5 {"The color is orange."} 
        6 {"The color is purple."} 
        7 {"The color is pink."}
        8 {"The color is brown."} 
        default {"The color could not be determined."}
    }
  $Usernames = @()

	While(1){
		$Username = Read-Host "`nEnter a username (or . to finish)"
		if($Username -eq "."){
			break
		}else{
		$Usernames += $Username	
		}
	}
Write-Progress -Activity "Update settings" -status $($Mailbox.Name) -percentComplete ([Int32](([Array]::IndexOf($Mailboxes, $Mailbox)/($Mailboxes.count))*100))
# Load
[xml]$XmlContent = (get-content $PathToXmlFile)

# Manipulate
$XmlContent.Content.Attribut1 = $Value

# Save
$XmlContent.Save($PathToXmlFile)
gci $SharePointBackupFolder -Recurse | where{$_.PsIsContainer} | %{
    gci $_.FullName | where{-not $_.PsIsContainer} | sort CreationTime -Descending | select -Skip 3 | Remove-Item -Force
}
Import-Module ServerManager
Add-Windowsfeature PowerShell-ISE
Get-ChildItem -Path $PSConfig.configs.Path -Filter Default.rdp -Recurse

# convert to utf8
(Get-ChildItem "test.csv") | %{if((Get-FileEncoding $_) -ne "UTF8"){Set-Content (Get-Content $_) -Encoding utf8 -Path $_}}

# export csv file
$Content | Export-Csv "test.csv" -Delimiter ";" -Encoding "UTF8" -NoTypeInformation

# import csv file
$Content = Import-CSV "test.csv" -Delimiter ";"
$Url = "https://server/file.ext"
$Path = "c:\downloads\file.ext"
[Net.ServicePointManager]::ServerCertificateValidationCallback = {$true}
$webClient = new-object System.Net.WebClient
$webClient.DownloadFile( $Url, $Path )
cmdkey /list | ForEach-Object{if($_ -like "*Ziel:*"){cmdkey /del:($_ -replace " ","" -replace "Ziel:","")}}
if(!(Test-Path -path $LogPath)){New-Item $LogPath -Type Directory}
$Objects = @(

    $(New-Object PSObject -Property @{
        Name = "Objekt1"
        Value = "value1","value2"
    }),
    $(New-Object PSObject -Property @{
        Name = "Objekt2"
        Value = "value1","value2"
    })

)

$Objects2 = @(

     @{
        Name = "Objekt3"
        Value = "value1","value2"
    },
     @{
        Name = "Objekt4"
        Value = "value1","value2"
    }

)
$FilePath = "FILENAME" 
Set-Content (Get-Content $FilePath) -Encoding utf8 -Path $FilePath
# Check for executeable
if ((Get-Command "cmdkey.exe") -and (Get-Command "mstsc.exe"))  { }

# Execute with Powershell version 2 instead of version 3 and higher
if($Host.Version.Major -gt 2){
  powershell -Version 2 $MyInvocation.MyCommand.Definition
	exit
}

# only powershell 2 and higher
if($Host.Version.Major -lt 2){
    throw "Only compatible with Powershell version 2 and higher"
}else{
}