Today I’m going to play around with functions.

I use functions to do repetitive admin tasks to make my life easier.

Functions can be simple and complex depending on your need, sometimes it’s static and won’t need any dynamic parameters.

Function Reset-WUFolder {
# Stop Windows update and BITS services
Stop-Service -Name wuauserv
Stop-Service -Name BITS
# Remove WU Folder
Remove-Item -Path $env:SystemRoot\SoftwareDistribution -Force
# Start Windows update and BITS services again
Start-Service -Name wuauserv
Start-Service -Name BITS

Run the code and you can call your function from the command line.


Sometimes you want to use dynamic variables in your functions.

Function Copy-TestDocument {
# Setup parameters
# Check if the TempFolder exists
if(!(Test-Path -Path $TempFolder)) {
New-Item $TempFolder -ItemType Directory
# Copy the file from FileLocation variable and output into logfile inside the TempFolder
Copy-Item $FileLocation -Destination $TempFolder -Verbose 4> $TempFolder\LogFile.txt

Then run it from the command line

Copy-TestDocument -TempFolder "C:\TempFolder" -FileLocation "C:\temp\test.txt"

These functions are pretty simple but can be very useful and fairly easy to understand and use.

Some functions you need to define the type of parameter and if you want to make it a mandatory or even have a switch enabled.

Function Display-Types {
[parameter(Mandatory=$true)][DateTime] $Date,
[parameter(Mandatory=$true)][string] $String,
[parameter(Mandatory=$false)][Switch] $Log
if($log) {
$Date | Out-File C:\temp\log.txt -Append
$String | Out-File C:\temp\log.txt -Append
} else {

Then run it in command line

Display-Types -Date (Get-Date).ToString() -String "Test String" -Log

As you can see that playing with functions can be fun to play around with.
I’ll not be going into advanced functions yet as I will go into that later in my series.

I hope you can enjoy and find this useful and don’t hesitate to be in contact if you want to know more or if I can help you write a successful function or whole scripts.

That’s it for now,
Hallgrimur #TheMachine