profile for Gajendra D Ambi on Stack Exchange, a network of free, community-driven Q&A sites

Wednesday, May 31, 2017

Powershell : Auto Intialize, Add, Format disks at log in

There are many scenarios where you have mass deployed VMs or present disks to users in a cloud environment where it is necessary for users to initialize, add, format the drives but it won't be wise to ask all users to do this. nobody wants to. Hence this.

Get-Disk | Where partitionstyle -eq ‘raw’ | Initialize-Disk -PartitionStyle MBR -PassThru | New-Partition -AssignDriveLetter -UseMaximumSize | Format-Volume -FileSystem NTFS -NewFileSystemLabel "$env:UserName" -Confirm:$false

  1. Create your .ps1 script and place it in a folder.  The path would look something like this:
  1. Create a .cmd file and place it in
C:\Users\<user_name>\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup\startup.cmd
 In this startup.cmd file
PowerShell -Command "Set-ExecutionPolicy Unrestricted" >> "%TEMP%\StartupLog.txt" 2>&1 
PowerShell C:\script.ps1 >> "%TEMP%\StartupLog.txt" 2>&1