Last active
December 18, 2015 07:49
-
-
Save janikvonrotz/5749452 to your computer and use it in GitHub Desktop.
PowerShell: Snippets
#PowerShell
#SnippetContainer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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{ | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$FilePath = "FILENAME" | |
Set-Content (Get-Content $FilePath) -Encoding utf8 -Path $FilePath |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$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" | |
} | |
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if(!(Test-Path -path $LogPath)){New-Item $LogPath -Type Directory} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
cmdkey /list | ForEach-Object{if($_ -like "*Ziel:*"){cmdkey /del:($_ -replace " ","" -replace "Ziel:","")}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$Url = "https://server/file.ext" | |
$Path = "c:\downloads\file.ext" | |
[Net.ServicePointManager]::ServerCertificateValidationCallback = {$true} | |
$webClient = new-object System.Net.WebClient | |
$webClient.DownloadFile( $Url, $Path ) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 ";" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Write-Host "Finished" -ForegroundColor Green | |
Read-Host "Press Enter to exit" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Get-ChildItem -Path $PSConfig.configs.Path -Filter Default.rdp -Recurse |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Import-Module ServerManager | |
Add-Windowsfeature PowerShell-ISE |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
gci $SharePointBackupFolder -Recurse | where{$_.PsIsContainer} | %{ | |
gci $_.FullName | where{-not $_.PsIsContainer} | sort CreationTime -Descending | select -Skip 3 | Remove-Item -Force | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Load | |
[xml]$XmlContent = (get-content $PathToXmlFile) | |
# Manipulate | |
$XmlContent.Content.Attribut1 = $Value | |
# Save | |
$XmlContent.Save($PathToXmlFile) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Write-Progress -Activity "Update settings" -status $($Mailbox.Name) -percentComplete ([Int32](([Array]::IndexOf($Mailboxes, $Mailbox)/($Mailboxes.count))*100)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$Usernames = @() | |
While(1){ | |
$Username = Read-Host "`nEnter a username (or . to finish)" | |
if($Username -eq "."){ | |
break | |
}else{ | |
$Usernames += $Username | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$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