Skip to content

Instantly share code, notes, and snippets.

@janikvonrotz
Last active December 18, 2015 07:49
Show Gist options
  • Save janikvonrotz/5749452 to your computer and use it in GitHub Desktop.
Save janikvonrotz/5749452 to your computer and use it in GitHub Desktop.
PowerShell: Snippets #PowerShell #SnippetContainer
# 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{
}
$FilePath = "FILENAME"
Set-Content (Get-Content $FilePath) -Encoding utf8 -Path $FilePath
$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"
}
)
if(!(Test-Path -path $LogPath)){New-Item $LogPath -Type Directory}
cmdkey /list | ForEach-Object{if($_ -like "*Ziel:*"){cmdkey /del:($_ -replace " ","" -replace "Ziel:","")}}
$Url = "https://server/file.ext"
$Path = "c:\downloads\file.ext"
[Net.ServicePointManager]::ServerCertificateValidationCallback = {$true}
$webClient = new-object System.Net.WebClient
$webClient.DownloadFile( $Url, $Path )
# 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 ";"
Write-Host "Finished" -ForegroundColor Green
Read-Host "Press Enter to exit"
Get-ChildItem -Path $PSConfig.configs.Path -Filter Default.rdp -Recurse
Import-Module ServerManager
Add-Windowsfeature PowerShell-ISE
gci $SharePointBackupFolder -Recurse | where{$_.PsIsContainer} | %{
gci $_.FullName | where{-not $_.PsIsContainer} | sort CreationTime -Descending | select -Skip 3 | Remove-Item -Force
}
# Load
[xml]$XmlContent = (get-content $PathToXmlFile)
# Manipulate
$XmlContent.Content.Attribut1 = $Value
# Save
$XmlContent.Save($PathToXmlFile)
Write-Progress -Activity "Update settings" -status $($Mailbox.Name) -percentComplete ([Int32](([Array]::IndexOf($Mailboxes, $Mailbox)/($Mailboxes.count))*100))
$Usernames = @()
While(1){
$Username = Read-Host "`nEnter a username (or . to finish)"
if($Username -eq "."){
break
}else{
$Usernames += $Username
}
}
$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."}
    }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment