Create folder and file for a new project using powershell

posted in: Powershell | 0

My objective is to create a folder with a desktop.ini that is used by my colleagues to store files for a project. Usually I create this manually on the correct file server and copy an existing desktop.ini which I edit and then set the attribute to System.

I like to work with scripts and functions so I created the function below.

function New-ProjectFolder {

[CmdletBinding()]

param(
[Parameter(Mandatory=$True)]

[string[]

]$ProjectNr, [Parameter()]

[string[]

]$Description ) BEGIN{} PROCESS{ Write-Verbose “Creating PSDrive PRoot” New-PSDrive -Name PRoot -PSProvider FileSystem -Root \\servername\share | Out-Null Write-Verbose “Creating Project folder $ProjectNr” New-Item -Path PRoot:\$ProjectNr -ItemType directory -Force | Out-Null Write-Verbose “Copy Desktop.ini to $ProjectNr” Copy-Item -Path .\desktop.ini -Destination PRoot:\$ProjectNr\desktop.ini Write-Verbose “Setting LocalizedResourceName to $Description” (Get-Content PRoot:\$ProjectNr\desktop.ini) | ForEach-Object {$_ -replace “LocalizedResourceName=”, “LocalizedResourceName=$ProjectNr – $Description”} | Set-Content PRoot:\$ProjectNr\desktop.ini Write-Verbose “Setting attribute to System” Set-ItemProperty PRoot:\$ProjectNr -name Attributes -Value ([System.IO.FileAttributes]::System) } END{} }

Note that I run the script from the same folder where the desktop.ini is located.

This isn’t perfect yet though. I want to specify the server name and path as a parameter but for now I can’t get UNC to work like it want it to. Lets call this a work in progress.

Projectfolder

Leave a Reply

Your email address will not be published. Required fields are marked *