{"id":83296,"date":"2018-09-15T00:00:32","date_gmt":"2018-09-15T07:00:32","guid":{"rendered":"http:\/\/devblogs.microsoft.com\/scripting\/?p=83296"},"modified":"2019-02-18T09:09:53","modified_gmt":"2019-02-18T16:09:53","slug":"using-powershell-to-create-a-folder-of-demo-data","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/using-powershell-to-create-a-folder-of-demo-data\/","title":{"rendered":"Using PowerShell to create a folder of Demo data"},"content":{"rendered":"<p><strong>Summary<\/strong>: Creating sample files with random sizes and dates for use in a Lab<\/p>\n<p>Q: Hey, Dr. Scripto!<\/p>\n<p>From time to time I like to show \u201chow things works\u201d at User Group meetings and to colleagues.\u00a0\u00a0 But I don\u2019t always have a good pile of sample files.\u00a0\u00a0 Do you have any examples of how to create some \u201cDemo data\u201d?<\/p>\n<p>\u2014SH<\/p>\n<p>A: Hello SH,<\/p>\n<p>I actually ran into the same problem a few weeks ago when I was writing an article.\u00a0\u00a0 I needed exactly that!<\/p>\n<p>What I needed was a folder full of sample files with not only random file names, but random content and even random dates.<\/p>\n<p>Just *how* could I achieve that?<\/p>\n<p>The first thought I had was \u201cJust generate random filenames of numbers and put in a generic bit of content.\u201d<\/p>\n<p>This simple solution would be a start<\/p>\n<p><strong># Provide Folder name and create\n<\/strong><strong>$Folder=&#8217;C:\\Demo&#8217;<\/strong><\/p>\n<p><strong>New-Item -ItemType Directory -Path $Folder<\/strong><\/p>\n<p><strong># Create a series of 10 files\n<\/strong><strong>for ($x=0;$x -lt 10; $x++)<\/strong><\/p>\n<p><strong>{\n<\/strong><strong># Let&#8217;s create a completely random filename\n<\/strong><strong>$filename=&#8221;$($Folder)\\$((Get-Random 100000).tostring()).txt&#8221;<\/strong><\/p>\n<p><strong># Now we&#8217;ll create the file with some content\n<\/strong><strong>Add-Content -Value &#8216;Just a simple demo file&#8217; -Path $filename<\/strong><\/p>\n<p><strong>}<\/strong><\/p>\n<p>There! A repeatable solution to create 10 simple files!<\/p>\n<p>Oh but wait, they were two problems I saw. Both were irritating.<\/p>\n<ul>\n<li>Each file was the same size<\/li>\n<li>Each filename was just a number followed by txt<\/li>\n<\/ul>\n<p>These things I could improve.\u00a0\u00a0 I decided with preclude the number with a name like \u2018LogFile\u2019 so at least they *could* look a bit more legitimate.<\/p>\n<p>For content\u2026. How could I solve that?<\/p>\n<p>I decided on a second loop of random limits and to populate it with ASCII values.<\/p>\n<p>The useful values in <a href=\"https:\/\/en.wikipedia.org\/wiki\/ASCII\">ASCII<\/a> (vs. PetSCII or ATASCII if you\u2019re REALLY a Nerd from the \u201880\u2019s) numbered from 32 to 96.\u00a0\u00a0 Those values would give me everything from a blank space to a little \u201cBacktick\u201d<\/p>\n<p>Get-Random would work but it would start from 0 to a number. I needed to ensure I picked a numeric range between 32 and 96.<\/p>\n<p>This was solved using this statement<\/p>\n<p><strong>(Get-Random 64)+32<\/strong><\/p>\n<p>Now the only challenge was to convert the Number to a Character.\u00a0\u00a0 You can convert an ASCII character to it\u2019s numeric equivalent using the following statement. (Here we convert the letter \u2018A\u2019 to it\u2019s ASCII equivalent).<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/pic1.png\"><img decoding=\"async\" class=\"alignnone wp-image-83335\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/pic1.png\" alt=\"\" width=\"430\" height=\"75\" \/><\/a><\/p>\n<p>We can REVERSE the process by swapping them around and providing the numeric equivalent.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/pic2.png\"><img decoding=\"async\" class=\"alignnone wp-image-83345\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/pic2.png\" alt=\"\" width=\"443\" height=\"75\" \/><\/a><\/p>\n<p>To get my Random Number as character (Provide I have a range of values between 0 and 255 as a result) I would use this statement.<\/p>\n<p><strong>[char][byte]((Get-Random 64)+32)<\/strong><\/p>\n<p>So create a random string of content I just created a simple loop such as this.<\/p>\n<p><strong># We&#8217;re going to build files up to 1K in size<\/strong><\/p>\n<p><strong>\u00a0\u00a0 $limit=(Get-random 1024)<\/strong><\/p>\n<p><strong>\u00a0\u00a0\u00a0\u00a0 # Let&#8217;s build the random content\n<\/strong><strong>\u00a0\u00a0 for($y=0;$y -lt $limit;$y++)\n<\/strong><strong>\u00a0\u00a0 {<\/strong><\/p>\n<p><strong>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 # We&#8217;re building a content of pure ASCII data\n<\/strong><strong>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 $a=$a+[char][byte]((Get-Random 64)+32)<\/strong><\/p>\n<p><strong>\u00a0\u00a0 }<\/strong><\/p>\n<p>Ok\u2026 it didn\u2019t produce deep reading material, but I could produce random content up to 1 kilobyte in size to replace for the little string that read \u2018Just a simple demo file\u2019<\/p>\n<p>To alter the filename to have \u201cLogfile\u201d at the beginning I simply injected the name into the string like this.<\/p>\n<p><strong>$filename=&#8221;$($Folder)\\Logfile$((Get-Random 100000).tostring()).txt&#8221;<\/strong><\/p>\n<p>If you\u2019re new to PowerShell I could also have put the pieces of the string together individually like this.<\/p>\n<p><strong>$filename=$Folder+&#8217;\\Logfile&#8217;+((Get-Random 100000).tostring())+&#8217;.txt&#8217;<\/strong><\/p>\n<p>Let\u2019s review<\/p>\n<ul>\n<li>Each file now has a unique size and content<\/li>\n<li>Each filename now has something a little more to the name<\/li>\n<\/ul>\n<p>I was sitting there pleased with myself. I could easily alter the value and have Thousands of files.<\/p>\n<p>\u201cMuah ha ha ha haaaa!\u201d I cackled like Doctor Frankenstein who is about to give life to the monster!<\/p>\n<p>\u2026.and then (don\u2019t you those?)<\/p>\n<p>I realized every single file was just a sequence of the same date and time.\u00a0\u00a0 I wanted to be able to have content that had random dates and times.<\/p>\n<p>For this I had to go up a level in permissions with using Administrative rights (Remember this is for DEMO content)<\/p>\n<p>I knew with PowerShell I can access and CHANGE the current date and time with the following two Cmdlets.<\/p>\n<p><strong>Get-Date<\/strong><\/p>\n<p><strong>Set-Date<\/strong><\/p>\n<p>I also knew I could adjust the Date and Time in precise increments in the following fashion<\/p>\n<p><strong>$TimeDifference=New-TimeSpan -Minutes 30<\/strong><\/p>\n<p><strong>Set-Date -Adjust $TimeDifference<\/strong><\/p>\n<p>Now the wheels were churning.\u00a0\u00a0 I needed a loop to do the following<\/p>\n<ul>\n<li>Pick a random number of days, hours and minutes (and remember them!)<\/li>\n<li>Create a Timespan<\/li>\n<li>Adjust the Set-Date with said Timespan<\/li>\n<li>Once time was adjusted, create my file (Which would be stamped with this random date)<\/li>\n<li>Create a \u201cNegative Timespan\u201d to undo my playing with the Clock<\/li>\n<li>Correct the Date.<\/li>\n<\/ul>\n<p>Now I\u2019m certain many of you have access to a T.A.R.D.I.S. but good old Dr. Scripto doesn\u2019t, so here at\u00a0the Scripting Blog we use PowerShell.<\/p>\n<p>Here\u2019s an example of that very code that adjusts the time, creates the file and then Sets time back. Or mostly back at least.<\/p>\n<p><strong>\u00a0\u00a0 $DaysToMove=((Get-Random 120) -60)<\/strong>\n<strong>\u00a0\u00a0 $HoursToMove=((Get-Random 48) -24)<\/strong>\n<strong>\u00a0\u00a0 $MinutesToMove=((Get-Random 120) -60)<\/strong>\n<strong>\u00a0\u00a0 $TimeSpan=New-TimeSpan -Days $DaysToMove -Hours $HoursToMove -Minutes $MinutesToMove<\/strong><\/p>\n<p><strong>\u00a0\u00a0 # Now we adjust the Date and Time by the new TimeSpan<\/strong>\n<strong>\u00a0\u00a0 # Needs Admin rights to do this as well!<\/strong><\/p>\n<p><strong>\u00a0\u00a0\u00a0\u00a0 Set-Date -Adjust $Timespan | Out-Null<\/strong><\/p>\n<p><strong>\u00a0\u00a0 # Create that file<\/strong>\n<strong>\u00a0\u00a0 Add-Content -Value $a -Path $filename<\/strong><\/p>\n<p><strong>\u00a0\u00a0 # Now we REVERSE the Timespan by the exact same amount<\/strong>\n<strong>\u00a0\u00a0 $TimeSpan=New-TimeSpan -Days (-$DaysToMove) -Hours (-$HoursToMove) -Minutes (-$MinutesToMove)<\/strong><\/p>\n<p><strong>\u00a0\u00a0 Set-Date -Adjust ($Timespan) | Out-Null<\/strong><\/p>\n<p>Now pleased with the results\u2026. The Script was re-run<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/pic3.png\"><img decoding=\"async\" class=\"alignnone wp-image-83355\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/pic3-1024x577.png\" alt=\"\" width=\"738\" height=\"416\" \/><\/a><\/p>\n<p>There are other things I could do to this script, such as have it test for existence of the Demo folder or ensure filenames were never duplicated.\u00a0\u00a0 But this was to create some basic \u201cdummy Data\u201d.<\/p>\n<p>If you have to create a demo environment, you can use similar techniques to these for populating Active Directory users or SQL tables as well.<\/p>\n<p>I hope this was a fun and informative read for you today and that you\u2019re enjoying the weather as well!<\/p>\n<p>I invite you to follow the Scripting Guys on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\">Twitter<\/a> and <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\">Facebook<\/a>. If you have any questions, send email to them at <u>scripter@microsoft.com<\/u>, or post your questions on the <a href=\"http:\/\/bit.ly\/scriptingforum\">Official Scripting Guys Forum<\/a>. See you tomorrow.<\/p>\n<p>Until we meet again, just remember we can all change the one world with just one Cmdlet at a time!<\/p>\n<p>Cheers!<\/p>\n<p><strong>Sean Kearney, Premier Field Engineer, Hey Scripting Guy<\/strong><\/p>\n<p>Windows PowerShell, Sean Kearney, Hey Scripting Guy<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Creating sample files with random sizes and dates for use in a Lab<\/p>\n","protected":false},"author":596,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[568,685],"tags":[3,154,45],"class_list":["post-83296","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-hey-scripting-guy","category-scripting-techniques","tag-scripting-guy","tag-sean-kearney","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Creating sample files with random sizes and dates for use in a Lab<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/83296","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/596"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=83296"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/83296\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=83296"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=83296"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=83296"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}