From the Super Mario Wiki
Space Junk are special platforms appearing in Super Mario Galaxy and Super Mario Galaxy 2. They come in different varieties, including red or brown octagons, purple cubes, and green and blue rectangular platforms, which are the most common. Normally, they will float around randomly in space; however, when Mario or Luigi comes near them, they will suddenly move into place around him, creating a path for him above a bottomless pit.
In Bowser's Galaxy Reactor, Space Junk appears in the Ice Planet and fifth area, and are the only way to traverse both areas. This Space Junk is different, as they are fragile and will shatter several seconds after making the path. In the Ice Planet, the Space Junk is cube-shaped and made of ice, as Mario can skate on them. In the fifth area, which is comprised almost entirely of Space Junk floating in space, has platforms that are shaped like a rectangular prism. Mario must walk along these platforms in 2-D, while avoiding Firebars and encountering Gravity Walls.
In Super Mario Galaxy 2, Space Junk appears in the Cloudy Court Galaxy and Rolling Masterpiece Galaxy. Space Junk appears only in the Cosmic Clones mission in Cloudy Court Galaxy, where they form paths for Mario in lieu of the Cloud Flower. Mario also encounters Space Junk acting as walls, as Mario needs to Wall Jump up a specific area instead of using clouds. Only the green-and-blue rectangular variety of Space Junk appeared here.
In the Rolling Masterpiece Galaxy, Space Junk appears on the Palette Planet. Mario must ride the Star Ball across a short pathway of these platforms. Again, only the green-and-blue rectangular variety appear here.