{"id":1033,"date":"2021-12-31T16:18:55","date_gmt":"2021-12-31T15:18:55","guid":{"rendered":"https:\/\/vroamam.com\/wordpress\/?p=1033"},"modified":"2021-12-31T18:10:49","modified_gmt":"2021-12-31T17:10:49","slug":"psdrives","status":"publish","type":"post","link":"https:\/\/vroamam.com\/wordpress\/blog\/psdrives\/","title":{"rendered":"PSDrives"},"content":{"rendered":"\n<p>PSDrives are the presentation of a connected datastore by a PSProvider. They are repesented by a name.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Provider<\/th><th>Drive(s)<\/th><\/tr><\/thead><tbody><tr><td>Alias<\/td><td>Alias:<\/td><\/tr><tr><td>Certificate<\/td><td>Cert:<\/td><\/tr><tr><td>Environment<\/td><td>Env:<\/td><\/tr><tr><td>FileSystem<\/td><td>C: (*)<\/td><\/tr><tr><td>Function<\/td><td>Function:<\/td><\/tr><tr><td>Registry<\/td><td>HKLM: HKCU:<\/td><\/tr><tr><td>Variable<\/td><td>Variable:<\/td><\/tr><tr><td>WSMan<\/td><td>WSMan:<\/td><\/tr><\/tbody><\/table><figcaption>Default PSProviders with their Name<\/figcaption><\/figure>\n\n\n\n<p>First of all lets see what PS Drives we have available<\/p>\n\n\n\n<p><code>Get-PSDrive<\/code><\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"548\" height=\"344\" src=\"https:\/\/vroamam.com\/wordpress\/wp-content\/uploads\/2021\/12\/image-6.png\" alt=\"\" class=\"wp-image-1034\"\/><figcaption>Screen capture showing the otput of Get-PSDrive<\/figcaption><\/figure>\n\n\n\n<p>The capture above shows the PSDrives on my machine and you can see all the defaults listed, <\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Alias<\/li><li>FileSystem<\/li><li>Certificate<\/li><li>Registry<\/li><li>Variable<\/li><li>Function<\/li><li>WSMan.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">FileSystem<\/h2>\n\n\n\n<p>Let&#8217;s start with the file system, this is the most commonly used and probably the most familiar. As you can see below the file system name (in windows) is equal to its drive letter, you can address any drive on the system in PowerShell this way.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"434\" height=\"240\" src=\"https:\/\/vroamam.com\/wordpress\/wp-content\/uploads\/2021\/12\/image-21.png\" alt=\"\" class=\"wp-image-1055\"\/><figcaption>Screen capture showing the output of <code>Get-PSProvider<\/code> but only selecting the Filesystem provider<\/figcaption><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Names and Aliases<\/h2>\n\n\n\n<p>Note how all of the Filesystem drive names are linked to their drive letter (at least in Windows). Now you can use all your old cmd.exe commands in here just as with a traditional file system, things like <code>cd<\/code> or <code>dir<\/code> all work<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"522\" height=\"298\" src=\"https:\/\/vroamam.com\/wordpress\/wp-content\/uploads\/2021\/12\/image-7.png\" alt=\"\" class=\"wp-image-1035\"\/><figcaption>Screen capture showing the use of <code>cd<\/code> and <code>dir<\/code> commands in a PowerShell session<\/figcaption><\/figure>\n\n\n\n<p>what is less obvious here is that these are actually Aliases to PowerShell cmdlets<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Change directory &#8211; <code>cd \\<\/code>  = <code>Set-Location C:\\<\/code><\/li><li>List Directory &#8211; <code>dir<\/code> = <code>Get-ChildItem<\/code><\/li><\/ul>\n\n\n\n<p>you can see all the aliases with the cmdlet <code>Get-Alias<\/code>  but to prove the point here let&#8217;s try and find the CD command. You should by now have a good guess that <code>get-alias cd<\/code> will provide what you need:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"619\" height=\"84\" src=\"https:\/\/vroamam.com\/wordpress\/wp-content\/uploads\/2021\/12\/image-8.png\" alt=\"\" class=\"wp-image-1036\"\/><figcaption>Screen capture showing the output of <code>Get-Alias cd<\/code><\/figcaption><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Using the PSDrive<\/h2>\n\n\n\n<p>So now we can access the drive using PowerShell cmdlets lets manipulate a file:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">New-Item C:\\FirstNewFile.txt\nAdd-Content C:\\FirstNewFile.txt \"This is the first addition\"\n\ncat C:\\FirstNewFile.txt <\/pre>\n\n\n\n<p>This creates a new file, adds a line of text to the file and then outputs the contents of the file to standard out (STDOUT). Note that  we use the Linux command <code>cat<\/code> to output the file contents but what are we really doing here? Yes of course we are using an alias, and we can see which one by using the get-alias command <code>Get-Alias cat<\/code>  which exposes the actual PS cmdlet as <code>Get-Content<\/code><\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"654\" height=\"87\" src=\"https:\/\/vroamam.com\/wordpress\/wp-content\/uploads\/2021\/12\/image-22.png\" alt=\"\" class=\"wp-image-1057\"\/><figcaption> Screen capture showing output of <code>Get-Alias cat<\/code><\/figcaption><\/figure>\n\n\n\n<p>We can further manipulate the data in the file. We can add additional text by using more <code>add-content<\/code> cmdlets<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"491\" height=\"24\" src=\"https:\/\/vroamam.com\/wordpress\/wp-content\/uploads\/2021\/12\/image-9.png\" alt=\"\" class=\"wp-image-1038\"\/><figcaption>Screen capture showing use of <code>add-content<\/code> cmdlet<\/figcaption><\/figure>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"353\" height=\"50\" src=\"https:\/\/vroamam.com\/wordpress\/wp-content\/uploads\/2021\/12\/image-10.png\" alt=\"\" class=\"wp-image-1039\"\/><figcaption>Screen capture showing output of <code>Get-Content<\/code> cmdlet demonstrating the second <code>Add-Content<\/code> had worked<\/figcaption><\/figure>\n\n\n\n<p>We can replace the data<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">set-content C:\\FirstNewFile.txt \"This is the replacement text\"<\/pre>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"545\" height=\"44\" src=\"https:\/\/vroamam.com\/wordpress\/wp-content\/uploads\/2021\/12\/image-11.png\" alt=\"\" class=\"wp-image-1040\"\/><figcaption>Screen capture showing the use of <code>Set-Content<\/code> to overwrite\\replace the contents of FirstNewFile.txt <\/figcaption><\/figure>\n\n\n\n<p>And finally lets remove the file we created using <code>Remove-Item<\/code><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Remove-Item C:\\FirstNewfile.txt<\/pre>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"592\" height=\"607\" src=\"https:\/\/vroamam.com\/wordpress\/wp-content\/uploads\/2021\/12\/image-12.png\" alt=\"\" class=\"wp-image-1041\"\/><figcaption>Screen captuire showing the rmeoval of the file FirstNeweFile.txt using the cmdlet Remove-Item<\/figcaption><\/figure>\n\n\n\n<p>In conclusion then, we can create items, list items, add content to items, replace content in items and remove items. So far we have done this using files, but the whole point of this section is that PSProviders present their connected data stores as drives so we can repeat this exercise on things such as the registry using the same or similar file manipulation cmdlets<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Manipulating the Registry<\/h2>\n\n\n\n<p>Lets consider how we can look at and manipulate the registry. I have created a dummy registry Key called vroamam at <\/p>\n\n\n\n<p><em><strong>Computer\\HKEY_LOCAL_MACHINE\\SOFTWARE\\vroamam <\/strong><\/em><\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1379\" height=\"783\" src=\"https:\/\/vroamam.com\/wordpress\/wp-content\/uploads\/2021\/12\/image-14.png\" alt=\"\" class=\"wp-image-1043\" srcset=\"https:\/\/vroamam.com\/wordpress\/wp-content\/uploads\/2021\/12\/image-14.png 1379w, https:\/\/vroamam.com\/wordpress\/wp-content\/uploads\/2021\/12\/image-14-1000x568.png 1000w, https:\/\/vroamam.com\/wordpress\/wp-content\/uploads\/2021\/12\/image-14-768x436.png 768w\" sizes=\"auto, (max-width: 1379px) 100vw, 1379px\" \/><figcaption>Screen Capture showing demo key and values in the windows registry (regedit)<\/figcaption><\/figure>\n\n\n\n<p>How would you list this key using PowerShell to see what values it held? Remember that we use the same cmdlets as we did for the file system so to list the contents of a folder\/diretcory we used Get-Content &#8230; we do the same here but with the path to the key instead of the folder.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Get-ChildItem HKLM:\\SOFTWARE\\vroamam<\/pre>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"511\" height=\"141\" src=\"https:\/\/vroamam.com\/wordpress\/wp-content\/uploads\/2021\/12\/image-18.png\" alt=\"\" class=\"wp-image-1049\"\/><figcaption>Screen capture sbhowing output of Get-ChildItem for  HKLM:\\SOFTWARE\\vroamam <\/figcaption><\/figure>\n\n\n\n<p>Just as we did with the file system content we can replace the content of the Property this time though we have to use a different cmdlet <code>Set-ItemProperty<\/code> and we provide it with a path to the item, the item name and a value<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"> set-itemproperty -Path HKLM:\\SOFTWARE\\vroamam\\PowerShellDemo -Name \"Demo String\" -Value \"Replaced Value\"<\/pre>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"892\" height=\"324\" src=\"https:\/\/vroamam.com\/wordpress\/wp-content\/uploads\/2021\/12\/image-17.png\" alt=\"\" class=\"wp-image-1047\" srcset=\"https:\/\/vroamam.com\/wordpress\/wp-content\/uploads\/2021\/12\/image-17.png 892w, https:\/\/vroamam.com\/wordpress\/wp-content\/uploads\/2021\/12\/image-17-768x279.png 768w\" sizes=\"auto, (max-width: 892px) 100vw, 892px\" \/><figcaption>Screen capture showing use of Set-ItemProperty with before and after listings using Get-ChildItem <\/figcaption><\/figure>\n\n\n\n<p>Usign the cmdlets you can create new items, set new item property values and rmeove items. Just the same way as we did with the file system.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Environment<\/h2>\n\n\n\n<p>And finally, to prove the point, you can do the same with other data stores such as environment variables.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Get-ChildItem Env:*<\/pre>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1346\" height=\"702\" src=\"https:\/\/vroamam.com\/wordpress\/wp-content\/uploads\/2021\/12\/image-19.png\" alt=\"\" class=\"wp-image-1050\" srcset=\"https:\/\/vroamam.com\/wordpress\/wp-content\/uploads\/2021\/12\/image-19.png 1346w, https:\/\/vroamam.com\/wordpress\/wp-content\/uploads\/2021\/12\/image-19-1000x522.png 1000w, https:\/\/vroamam.com\/wordpress\/wp-content\/uploads\/2021\/12\/image-19-768x401.png 768w\" sizes=\"auto, (max-width: 1346px) 100vw, 1346px\" \/><figcaption>Screen capture showing a list of environment varaiables from Get-ChildItem Env:*<\/figcaption><\/figure>\n\n\n\n<p>If you want to see the value of a single variable<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">get-Item env:computername<\/pre>\n\n\n\n<p>To set a new value or replace an existing value<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">set-content -Path PowerShellDemo -Value 'Initial Value'\nset-content -Path PowerShellDemo -Value 'Replaced Value'<\/pre>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"556\" height=\"230\" src=\"https:\/\/vroamam.com\/wordpress\/wp-content\/uploads\/2021\/12\/image-20.png\" alt=\"\" class=\"wp-image-1051\"\/><figcaption>Screen capture showing the output and operation of Set-Location, Set-Content and Get-Item cmdlets <\/figcaption><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>Hopefully you can see that you can manipulate a variety of system data stores using PowerShell Providers and PowerShell Drives using a simple file system approach. Up Next &#8211; Using WMI and CIM<\/p>\n","protected":false},"excerpt":{"rendered":"<p>PSDrives are the presentation of a connected datastore by a PSProvider. They are repesented by a name. Provider Drive(s) Alias Alias: Certificate Cert: Environment Env: FileSystem C: (*) Function Function: Registry HKLM: HKCU: Variable Variable: WSMan WSMan: Default PSProviders with their Name First of all lets see what PS Drives we have available Get-PSDrive The [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"ocean_post_layout":"","ocean_both_sidebars_style":"","ocean_both_sidebars_content_width":0,"ocean_both_sidebars_sidebars_width":0,"ocean_sidebar":"0","ocean_second_sidebar":"0","ocean_disable_margins":"enable","ocean_add_body_class":"","ocean_shortcode_before_top_bar":"","ocean_shortcode_after_top_bar":"","ocean_shortcode_before_header":"","ocean_shortcode_after_header":"","ocean_has_shortcode":"","ocean_shortcode_after_title":"","ocean_shortcode_before_footer_widgets":"","ocean_shortcode_after_footer_widgets":"","ocean_shortcode_before_footer_bottom":"","ocean_shortcode_after_footer_bottom":"","ocean_display_top_bar":"default","ocean_display_header":"default","ocean_header_style":"","ocean_center_header_left_menu":"0","ocean_custom_header_template":"0","ocean_custom_logo":0,"ocean_custom_retina_logo":0,"ocean_custom_logo_max_width":0,"ocean_custom_logo_tablet_max_width":0,"ocean_custom_logo_mobile_max_width":0,"ocean_custom_logo_max_height":0,"ocean_custom_logo_tablet_max_height":0,"ocean_custom_logo_mobile_max_height":0,"ocean_header_custom_menu":"0","ocean_menu_typo_font_family":"0","ocean_menu_typo_font_subset":"","ocean_menu_typo_font_size":0,"ocean_menu_typo_font_size_tablet":0,"ocean_menu_typo_font_size_mobile":0,"ocean_menu_typo_font_size_unit":"px","ocean_menu_typo_font_weight":"","ocean_menu_typo_font_weight_tablet":"","ocean_menu_typo_font_weight_mobile":"","ocean_menu_typo_transform":"","ocean_menu_typo_transform_tablet":"","ocean_menu_typo_transform_mobile":"","ocean_menu_typo_line_height":0,"ocean_menu_typo_line_height_tablet":0,"ocean_menu_typo_line_height_mobile":0,"ocean_menu_typo_line_height_unit":"","ocean_menu_typo_spacing":0,"ocean_menu_typo_spacing_tablet":0,"ocean_menu_typo_spacing_mobile":0,"ocean_menu_typo_spacing_unit":"","ocean_menu_link_color":"","ocean_menu_link_color_hover":"","ocean_menu_link_color_active":"","ocean_menu_link_background":"","ocean_menu_link_hover_background":"","ocean_menu_link_active_background":"","ocean_menu_social_links_bg":"","ocean_menu_social_hover_links_bg":"","ocean_menu_social_links_color":"","ocean_menu_social_hover_links_color":"","ocean_disable_title":"default","ocean_disable_heading":"default","ocean_post_title":"","ocean_post_subheading":"","ocean_post_title_style":"","ocean_post_title_background_color":"","ocean_post_title_background":0,"ocean_post_title_bg_image_position":"","ocean_post_title_bg_image_attachment":"","ocean_post_title_bg_image_repeat":"","ocean_post_title_bg_image_size":"","ocean_post_title_height":0,"ocean_post_title_bg_overlay":0.5,"ocean_post_title_bg_overlay_color":"","ocean_disable_breadcrumbs":"default","ocean_breadcrumbs_color":"","ocean_breadcrumbs_separator_color":"","ocean_breadcrumbs_links_color":"","ocean_breadcrumbs_links_hover_color":"","ocean_display_footer_widgets":"default","ocean_display_footer_bottom":"default","ocean_custom_footer_template":"0","ocean_post_oembed":"","ocean_post_self_hosted_media":"","ocean_post_video_embed":"","ocean_link_format":"","ocean_link_format_target":"self","ocean_quote_format":"","ocean_quote_format_link":"post","ocean_gallery_link_images":"off","ocean_gallery_id":[],"footnotes":""},"categories":[114,3],"tags":[81,11],"class_list":["post-1033","post","type-post","status-publish","format-standard","hentry","category-powershell","category-training","tag-powershell","tag-training","entry"],"_links":{"self":[{"href":"https:\/\/vroamam.com\/wordpress\/wp-json\/wp\/v2\/posts\/1033","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/vroamam.com\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/vroamam.com\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/vroamam.com\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/vroamam.com\/wordpress\/wp-json\/wp\/v2\/comments?post=1033"}],"version-history":[{"count":7,"href":"https:\/\/vroamam.com\/wordpress\/wp-json\/wp\/v2\/posts\/1033\/revisions"}],"predecessor-version":[{"id":1059,"href":"https:\/\/vroamam.com\/wordpress\/wp-json\/wp\/v2\/posts\/1033\/revisions\/1059"}],"wp:attachment":[{"href":"https:\/\/vroamam.com\/wordpress\/wp-json\/wp\/v2\/media?parent=1033"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/vroamam.com\/wordpress\/wp-json\/wp\/v2\/categories?post=1033"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/vroamam.com\/wordpress\/wp-json\/wp\/v2\/tags?post=1033"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}