Skip to content

Instantly share code, notes, and snippets.

@a5ync
a5ync / AddPath.ps1
Last active April 19, 2016 23:50
Adding a new path to environment variable Path (Machine or User space)
$newPath = "c:\scripts\"
Write-host Preparing environment
$currentPath = [Environment]::GetEnvironmentVariable("Path",[EnvironmentVariableTarget]::User) #or ::Machine
if ($currentPath -like '*'+$newPath+'*')
{
Write-host 'System environment "Path" already contains ' $newPath
}
else {
Write-host 'Adding ' $newPath ' to System "Path"'
@a5ync
a5ync / SetFullPermissionOnFiles.ps1
Created April 19, 2016 23:49
Set full permission on files from the given folder +filter [optional]
$user = "test"
$remoteFiles = Get-ChildItem "C:\img" -Filter *.png
foreach ($file in $remoteFiles)
{
Write-Host Changing permission of $file.FullName
#change permission
$Acl = Get-Acl $file.FullName
$Ar = New-Object System.Security.AccessControl.FileSystemAccessRule($user, "FullControl", "None", "None", "Allow")
$Acl.SetAccessRule($Ar)
@a5ync
a5ync / ChangeFileOwner.ps1
Created April 19, 2016 23:56
Changes the owner of files (filter *.png) in the given folder
# include Set-Owner
# https://gallery.technet.microsoft.com/scriptcenter/Set-Owner-ff4db177
. .\Set-Owner.ps1
$user = "testUser"
$remoteFiles = Get-ChildItem "C:\img" -Filter *.png
foreach ($file in $remoteFiles)
{
Write-Host Changing the owner of $file.FullName to $user
Set-Owner -Path $file.FullName -Account $user
@a5ync
a5ync / DownloadFromWebServer.ps1
Created April 20, 2016 00:01
Download web resources based on .txt file that contains url links
# download.txt
# http://fabricjs.com/assets/25.svg
# http://fabricjs.com/assets/36.svg
$destination="img"
New-Item -ItemType directory -force $destination
$links=Get-Content download.txt
foreach ($link in $links)
{
@a5ync
a5ync / DownloadGithubSnapshot.ps1
Created April 20, 2016 00:16
Download a lightweight Github .git-less snapshot (zipball) of a particular branch
$user = "user"
$pass= "password"
#zipballs of particular branches have syntax /repository-name/zipball/branch-name
$branchname = "master"
$linkToSnapshot = "https://github.com/a5ync/webgl/zipball/"+$branchname
#prepare credential header
$pair = "$($user):$($pass)"
$encodedCreds = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes($pair))
@a5ync
a5ync / CreateArchive.ps1
Last active April 20, 2016 00:19
Create/Extract an archive
$source = ".\img"
$destination = ".\deploy_" +[DateTime]::Now.ToString("yyyyMMdd-HHmmss")+".zip"
#compress
If(Test-path $destination) {Remove-item $destination}
Add-Type -assembly "system.io.compression.filesystem"
[io.compression.zipfile]::CreateFromDirectory($Source, $destination)
#extract
Expand-Archive $destination -dest .\test
@a5ync
a5ync / RemoveCommitName.ps1
Created April 20, 2016 00:40
Remove Github commit id(name) from zipball
#e.g. zipball\a5ync-webgl-68dda42
#get the first child folder of root
$zipballFolder = ".\zipball"
$targetFolder = ".\final"
$directoryWithNameOfCommit = Get-ChildItem $zipballFolder| Select-Object -first 1
Write-Host $directoryWithNameOfCommit
#move the folder content to a target folder
join-path $zipballFolder $directoryWithNameOfCommit |
@a5ync
a5ync / AddToAutostart.ps1
Created April 20, 2016 00:46
Add application to Windows auto-start (startup)
#update registry
Write-Host Adding to startup
$keyRun = 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Run'
$appToRun="C:\Windows\notepad.exe"
$label = "Hey Run Notepad"
Set-ItemProperty -Path $keyRun -Name $label -Value $appToRun
Write-Host Added to startup -foreground "green"
@a5ync
a5ync / Tail.ps1
Created April 20, 2016 00:47
Powershell version of linux tail
Get-Content myTestLog.log –Wait
#with filters
Get-Content myTestLog.log -wait | where { $_ -match “WARNING” }
@a5ync
a5ync / CheckService.ps1
Created April 20, 2016 00:51
Check if windows service is installed and running
$serviceName = "Dhcp"
$service = Get-Service $serviceName -ErrorAction SilentlyContinue
if($service)
{
Write-host service $service.Name is $service.Status
}
else
{
Write-host what service?