{"id":53613,"date":"2009-06-01T23:39:00","date_gmt":"2009-06-01T23:39:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2009\/06\/01\/hey-scripting-guy-how-can-i-work-with-the-offline-files-feature-in-windows\/"},"modified":"2009-06-01T23:39:00","modified_gmt":"2009-06-01T23:39:00","slug":"hey-scripting-guy-how-can-i-work-with-the-offline-files-feature-in-windows","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/hey-scripting-guy-how-can-i-work-with-the-offline-files-feature-in-windows\/","title":{"rendered":"Hey, Scripting Guy! How Can I Work with the Offline Files Feature in Windows?"},"content":{"rendered":"<p><H2><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Question\" border=\"0\" alt=\"Hey, Scripting Guy! Question\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" width=\"34\" height=\"34\"> <\/H2>\n<P>Hey, Scripting Guy! I have some critical files that I need to work with on a regular basis. In the office, the files are stored on a server. I need to be able to take the files home and work on them while working on my laptop. When I get back to work, I need the files to synchronize with the server. I know I need to use the offline files feature of my laptop, but I am not sure where the offline cache is located or whether or not it is enabled. Can I determine this information with a Windows PowerShell script?<BR><BR>&#8211; WB<\/P><IMG border=\"0\" alt=\"Spacer\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" width=\"5\" height=\"5\"><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Answer\" border=\"0\" alt=\"Hey, Scripting Guy! Answer\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" width=\"34\" height=\"34\"> \n<P>Hi WB,<\/P>\n<P>Scripting Guy Ed Wilson is feeling a little sluggish right now. He is foundered on Dungeness crab while he is visiting Seattle. He has been teaching a Windows PowerShell class to a group of Microsoft engineers, and he has been sampling the local cuisine. They offered to take him to a barbeque restaurant; however, being from an area where they claim to have invented barbeque, he politely declined and opted instead for a local seafood establishment. He is glad he made that choice\u2014sort of. Anyway, if we can get his attention (he is sitting in the corner groaning right now), we will ask him about your situation.<\/P>\n<P>Okay, just talked to Dungen-Ed. He suggested using a WMI class, <B>win32_OfflineFilesCache<\/B>, to get the information you require. He also wrote the <B>GetOffLineFiles.ps1<\/B> script for you. (Dude, he is groaning again).<\/P>\n<P><B>GetOffLineFiles.ps1<\/B><\/P><PRE class=\"codeSample\">param($computer=&#8221;localhost&#8221;, $help)<\/p>\n<p>function funline ($strIN)\n{\n $num = $strIN.length\n for($i=1 ; $i -le $num ; $i++)\n  { $funline += &#8220;=&#8221; }\n    Write-Host -ForegroundColor yellow $strIN \n    Write-Host -ForegroundColor darkYellow $funline\n}<\/p>\n<p>function funHelp()\n{\n$helpText=@&#8221;\nDESCRIPTION:\nNAME: GetOffLineFiles.ps1 \nPrints the offline files config on a local or remote machine.<\/p>\n<p>PARAMETERS: \n-computer Specifies name of the computer upon which to run the script\n-help     prints help file<\/p>\n<p>SYNTAX:\nGetOffLineFiles.ps1 -computer MunichServer<\/p>\n<p>Lists offline files config on a computer named MunichServer<\/p>\n<p>GetOffLineFiles.ps1 <\/p>\n<p>Lists offline files config on local computer<\/p>\n<p>GetOffLineFiles.ps1 -help ?<\/p>\n<p>Displays the help topic for the script<\/p>\n<p>&#8220;@\n$helpText\nexit\n}<\/p>\n<p>if($help){ funline(&#8220;Obtaining help &#8230;&#8221;) ; funhelp }<\/p>\n<p>$outtxt = Get-WmiObject -Class win32_OfflineFilesCache `\n        -computername $computer \nfunline(&#8220;Offline files configuration $env:computername&#8221;)<\/p>\n<p>format-table -Property active, enabled,location -autosize `\n       -inputobject $outtxt\n<\/PRE>\n<P>WB, offline file functionality provides automatic synchronization between files stored on a server and those stored on a laptop or other portable computing device. This provides a high level of robustness for the user and solves the backup problem of critical files. Additionally, it gives the user the chance to check in and to work with files. There is only one instance of the offline files cache. This offline file cache is seen in the following image, where we directly query the <B>Win32_OfflineFilesCache<\/B> WMI class. <\/P><IMG border=\"0\" alt=\"Image of the offline file cache\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/hsg\/2009\/june\/hey0602\/hsg-06-02-09-01.jpg\" width=\"500\" height=\"213\"> \n<P>&nbsp;<\/P>\n<P>However, there are a number of WMI classes supported by the WMI provider called, imaginatively enough, the <B>OfflineFilesWmiProvider<\/B>. On a computer running Windows 7, the following are the WMI classes related to offline files. <\/P>\n<UL>\n<LI>\n<DIV>Win32_OfflineFilesCache<\/DIV><\/LI>\n<LI>\n<DIV>Win32_OfflineFilesAssociatedItems<\/DIV><\/LI>\n<LI>\n<DIV>Win32_OfflineFilesChangeInfo<\/DIV><\/LI>\n<LI>\n<DIV>Win32_OfflineFilesItem<\/DIV><\/LI>\n<LI>\n<DIV>Win32_OfflineFilesPinInfo<\/DIV><\/LI>\n<LI>\n<DIV>Win32_OfflineFilesFileSysInfo<\/DIV><\/LI>\n<LI>\n<DIV>Win32_OfflineFilesSuspendInfo<\/DIV><\/LI>\n<LI>\n<DIV>Win32_OfflineFilesConnectionInfo<\/DIV><\/LI>\n<LI>\n<DIV>Win32_OfflineFilesDirtyInfo<\/DIV><\/LI>\n<LI>\n<DIV>Win32_PerfFormattedData_OfflineFiles_ClientSideCaching<\/DIV><\/LI>\n<LI>\n<DIV>Win32_PerfRawData_OfflineFiles_ClientSideCaching<\/DIV><\/LI>\n<LI>\n<DIV>Win32_PerfFormattedData_OfflineFiles_OfflineFiles<\/DIV><\/LI>\n<LI>\n<DIV>Win32_PerfRawData_OfflineFiles_OfflineFiles<\/DIV><\/LI><\/UL>\n<P>In the script, we wrote for you, we use the <B>WIN32_OfflineFilesCache<\/B> WMI class to determine three pieces of information. The first thing we want to know is if the offline files feature is enabled or not. If it is enabled, we need to know if it is active. If the offline files feature is enabled and active, we want to know where the files are stored. <\/P>\n<P>In the <B>GetOffLineFiles.ps1<\/B> script, we first need to define a couple of parameters. The first parameter we will use is the <B>\u2013computer<\/B> parameter. We use the <B>$computer<\/B> variable to hold the value supplied to the <B>\u2013computer<\/B> parameter. <B>Localhost<\/B> is assigned by default to enable the script to run against the local machine. The second parameter we define is <B>\u2013help<\/B>. We do not set the <B>$help<\/B> variable that receives values supplied to the <B>\u2013help<\/B> parameter from the command line to a default value because we are not interested in running Help all the time. The <B>param<\/B> statement is used to declare both of the command-line parameters. This line of code is seen here:<\/P><PRE class=\"codeSample\">param($computer=&#8221;localhost&#8221;, $help)\n<\/PRE>\n<P>Next, we need to define a couple of functions. The first function is the <B>funline<\/B> function. We will use the <B>funline<\/B> function to underline the header line for our output from the script. The <B>funline<\/B> function accepts a single input variable, which is called <B>$strIN<\/B>. The <B>$strIN<\/B> variable will hold the string value that is passed to it. It then uses the <B>length<\/B> property to determine how long the string is. We store this value in the <B>$num<\/B> variable. We next use the <B>for<\/B> statement to enter a loop that builds up a variable called <B>$funline<\/B> that will hold a series of equal signs (\u201c<B>=<\/B>\u201d) that is as long as the string contained in the <B>$strIN<\/B> variable. AFter we have created this line separator, we use two <B>Write-Host<\/B> cmdlets to display the string contained in the <B>$strIN<\/B> variable and the line separator contained in the <B>$funline<\/B> variable. We use contrasting colors for our two different <B>foregroundcolor<\/B> parameters. The <B>funline<\/B> function is seen here:<\/P><PRE class=\"codeSample\">function funline ($strIN)\n{\n $num = $strIN.length\n for($i=1 ; $i -le $num ; $i++)\n  { $funline += &#8220;=&#8221; }\n    Write-Host -ForegroundColor yellow $strIN \n    Write-Host -ForegroundColor darkYellow $funline\n}\n<\/PRE>\n<P>After the <B>funline<\/B> function is defined, we next define a function that will be used to display online Help data when the script is run with the <B>\u2013help<\/B> parameter specified. We begin the <B>funhelp<\/B> function by declaring the variable, <B>\u2013$helpText<\/B>, and assigning a <B>here-string<\/B> to it. In the <B>here-string<\/B>, we assign sections of text for the description, parameters, and command-line syntax. After we have completed the <B>here-string<\/B>, we display the contents of the <B>$helpText<\/B> variable, and then exit the script. The <B>funhelp<\/B> function is seen here:<\/P><PRE class=\"codeSample\">function funHelp()\n{\n$helpText=@&#8221;\nDESCRIPTION:\nNAME: GetOffLineFiles.ps1 \nPrints the offline files config on a local or remote machine.<\/p>\n<p>PARAMETERS: \n-computer Specifies name of the computer upon which to run the script\n-help     prints help file<\/p>\n<p>SYNTAX:\nGetOffLineFiles.ps1 -computer MunichServer<\/p>\n<p>Lists offline files config on a computer named MunichServer<\/p>\n<p>GetOffLineFiles.ps1 <\/p>\n<p>Lists offline files config on local computer<\/p>\n<p>GetOffLineFiles.ps1 -help ?<\/p>\n<p>Displays the help topic for the script<\/p>\n<p>&#8220;@\n$helpText\nexit\n}\n<\/PRE>\n<P>To determine if we display the contents of the Help file, we use an <B>if<\/B> statement to check for the presence of the <B>$help<\/B> variable. If the <B>$help<\/B> variable is present, it means the script was run with the <B>\u2013help<\/B> parameter specified. If this is the case, we first call the <B>funline<\/B> function to underline our status message, and then we call the <B>funhelp<\/B> function to display the Help file. This line of code is seen here:<\/P><PRE class=\"codeSample\">if($help){ funline(&#8220;Obtaining help &#8230;&#8221;) ; funhelp }\n<\/PRE>\n<P>Now we need to use the <B>WIN32_OfflineFilesCache<\/B> WMI class to retrieve the three pieces of information required for this script. To do this, we use the <B>Get-Wmiobject<\/B> cmdlet. We use the <B>\u2013class<\/B> parameter to tell WMI to query the <B>WIN32_OfflineFilesCache<\/B> class. We use the <B>\u2013computername<\/B> parameter to connect to a different computer, if required, and we store the resulting WMI object in the <B>$outtext<\/B> variable. This section of code is seen here:<\/P><PRE class=\"codeSample\">$outtxt = Get-WmiObject -Class win32_OfflineFilesCache `\n        -computername $computer\n<\/PRE>\n<P>Once we have retrieved the offline files configuration information, we use the <B>funline<\/B> function to print out a status message about the <B>$offline<\/B> folder. We use the environmental variable <B>computername<\/B> to retrieve the name of the current computer from the envirionmental PS drive <B>env:\\<\/B>. This line of code is seen here:<\/P><PRE class=\"codeSample\">funline(&#8220;Offline files configuration $env:computername&#8221;)\n<\/PRE>\n<P>Finally, we use the <B>Format-Table<\/B> cmdlet to format the output string. We choose the properties we need in the order we wish the data to be displayed. We use the <B>\u2013inputobject<\/B> parameter and supply the management object stored in the <B>$outtxt<\/B> variable to this parameter. We then choose the <B>\u2013autosize<\/B> parameter, so we will have a compact display on the screen. This section of code is seen here: <\/P><PRE class=\"codeSample\">format-table -Property active, enabled,location -autosize `\n       -inputobject $outtxt\n<\/PRE>\n<P>WB, we hope you will find the <B>GetOffLineFiles.ps1<\/B> script useful, and that it will assist you in determining the status of offline files on your computer. In addition, if you are ever in Seattle, Ed recommends that you do not attempt to eat a dozen Dungeness crabs for lunch by yourself. We hope to see you tomorrow. Until then keep on scripting.<\/P>\n<P><B>Ed Wilson and Craig Liebendorfer, Scripting Guys<\/B><\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! I have some critical files that I need to work with on a regular basis. In the office, the files are stored on a server. I need to be able to take the files home and work on them while working on my laptop. When I get back to work, I need [&hellip;]<\/p>\n","protected":false},"author":595,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[423,3,735,45],"class_list":["post-53613","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-backup-and-system-restore","tag-scripting-guy","tag-shadow-copy","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! I have some critical files that I need to work with on a regular basis. In the office, the files are stored on a server. I need to be able to take the files home and work on them while working on my laptop. When I get back to work, I need [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/53613","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\/595"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=53613"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/53613\/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=53613"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=53613"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=53613"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}