Run scripts in powershell

Powershell scripts by default is set to restricted. So if you try to run a powershell script on a new machine you’ll find that it doesn’t work.

So here are the commands I use most often.

First off the default command is

Set-ExecutionPolicy -ExecutionPolicy Restricted

This is the command I run if this is a powershell script that will be ran all the time on that computer

Set-ExecutionPolicy -ExecutionPolicy RemoteSigned

And this is the one I use to just run something one time

Set-ExecutionPolicy -Scope Process -ExecutionPolicy AllSigned

Batch file to sort files by name

So we have electronic journals that get dumped into a folder. These are things we need to keep. However having them all thrown into the same folder can also be a pain.  Not only does it just cause a bunch of files to be in one folder but I we need to look back at Jan 18th 2017 we’d have to sort through tons and tons of these files looking for the one we wanted.

So I wrote this bat file that basically looks at out file name and puts everything into it’s own separate folder.

So first I’ll post the code then I’ll explain.

@echo off

setlocal enabledelayedexpansion


pushd c:\sortfolder\

for /f "tokens=*" %%1 in ('dir /a-d /b c:\EJ\*.JRN') do (

set filename=%%1&set dirname=!filename:~0,2!\!filename:~2,2!\!filename:~4,2!

if not exist c:\sortedfolder\20!dirname!\ (md c:\sortedfolder\20!dirname!\)

move %%1 c:\sortedfolder\20!dirname!\>nul


Now out file format looks like this 18091700-000101Z.JRN

Now the only part we’re really interested in is

(180917) 00-000101Z.JRN

18 year, 09 month, 17 day.

Now this does all the sorting.

set filename=%%1&set dirname=!filename:~0,2!\!filename:~2,2!\!filename:~4,2!

!filename:~0,2! – Starts at the first character and goes two over so it grabs 18

!filename:~2,2! – Starts after the second character and goes 2 over so 09

!filename:~4,2! – Starts after the 4th character

If you adjust the first and last number you can sort by other setups.

So !filename:~0,4! Would start and go to include 1809

Purge old files and folders from directory

So at work we have a couple of directories that generate logs on a regular basis. These logs really are not that important. So I keep them for 60 days.

Problem of course is that some also create folders.

So I created this powershell script from some pieces of code I found that will go through the folder and clean out all the old files and then clean out the old folders.

I’m sure I could clean this up a bit, but seeing it does what I want now it’s good enough. You just need to change some of the variables

#Days older than

$HowOld = -60

#Path to the root folder

$Path = "C:\directory you want purged\"

#Deletion files task

get-childitem $Path -recurse | where {$_.lastwritetime -lt (get-date).adddays($HowOld) -and -not $_.psiscontainer} |% {remove-item $_.fullname -force -verbose}

#Deletion empty folders task

do {

$dirs = gci $Path -directory -recurse | Where { (gci $_.fullName -Force).count -eq 0 } | select -expandproperty FullName

$dirs | Foreach-Object { Remove-Item $_ }

} while ($dirs.count -gt 0)

Powershell script to backup hyper-v servers

So while I do not have a hyper-v server now I did have one. I cobbled together this script out of some stuff I found online to basically allow me to backup a hyper-v server (meaning not a windows server just straight hyper-v) to a backup server and keep a couple of copies in case a backup went bad.

So here’s the script.

# This script it designed to retrieve all virtual machines from a remote hyper-v server and export them to a location of your choosing.
# You can set this up as a scheduled task based on your needs. So if you need backups daily, weekly, you can set that based on your needs.
# It will then remove any backups older than the amount of backups you configure.

# Enter the name of the server you want to backup
$server = "HYPERV-SERVER"
# Enter the full path for the backup folder
$backuppath = "\\backup server\Hyper-V Backups" # Example: "\\backupserver\fileshare\HyperVBackups"
# How many backups of VMs you want to keep
$backupstokeep = 7

# Get all Hyper-V VMs
$vms = Get-VM -computername $hypervserver
# Grab the current date
$today = Get-Date -Format MM-dd-yy

foreach ($vm in $vms) {
$vmname = $vm.Name
Write-Host "Backing up $vmname..."
# Create the folders for the backup
New-Item -ItemType Directory -Path "$backuppath\$vmname" # We run this in case it is a new VM. Normally it will fail if the VM folder already exists, which is fine
New-Item -ItemType Directory -Path "$backuppath\$vmname\$today"
# Export the VM
Export-VM -VM $vm -Path "$backuppath\$vmname\$today"
# Remove any backups older than the past 7 days
Get-ChildItem "$backuppath\$vmname" | Sort-Object -Property CreationTime -Descending | Select-Object -Skip $backupstokeep | Remove-Item -Recurse

Worked great for my needs. Especially when the only other options I could find cost $$

Delete files older than… in powershell

I have a couple of directories that create files

Here’s a script I use for deleting files older than 30 days in powershell.

$Path = "C:\temp"
$Daysback = "-30"
$CurrentDate = Get-Date
$DatetoDelete = $CurrentDate.AddDays($Daysback)
Get-ChildItem $Path | Where-Object { $_.LastWriteTime -lt $DatetoDelete } | Remove-Item

You need to chance the $Path location and if you want do delete files after another time frame you just have to edit the 30 to however many days you want.